R Shiny中的DataTable回调行为 [英] DataTable callback behaviour in R Shiny

查看:100
本文介绍了R Shiny中的DataTable回调行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个找不到任何解决方案的问题。
我想使用DT :: datatable在闪亮的应用程序中显示表格。在此选项卡中,我想为某些由其坐标定义的单元格上色。这是其中彩色单元格对应于NA值的代码示例:

I have an issue for which I do not find any solution. I want to show a table in my shiny app with DT::datatable. In this tab, I want to color some cells which are defined by their coordinates. Here is an example of code where the colored cells corresponds to NA values :

test.table <- data.frame(lapply(1:8, function(x) {1:1000}))
test.table[c(2,3,7), c(2,7,6)] <- NA
id <- which(is.na(test.table))


datatable(test.table,
options = list(drawCallback=JS(
paste("function(row, data) {",
paste(sapply(1:ncol(test.table),function(i)
paste( "$(this.api().cell(",id %% nrow(test.table)-1,",",trunc(id / nrow(test.table))+1,").node()).css({'background-color': 'lightblue'});")
),collapse = "\n"),"}" ))
))

在R控制台(RStudio)中运行时,此代码可以正常工作,但是当我在闪亮的应用程序中实现此代码时,会出现一个小错误:首页上的彩色单元格位于正确的地方,但是当我单击下一个按钮查看其他页面时,有色单元格似乎没有更新,即使没有NA,它们仍然是有色的。
以下是该问题的有效示例:

This code works fine when run in a R console (RStudio) but when I implement this in my shiny app, there is a little bug : on the first page, the colored cells are at the right place but when I click on the next button to view the other pages,it seems that the colored cells do nout update and they are still colored even is there are no NA anymore. Here is a working example on that problem:

shinyApp(
ui = fluidPage(
    fluidRow(
        column(12,
        dataTableOutput('table')
       )
)
),
server = function(input, output) {
  test.table <- data.frame(lapply(1:8, function(x) {1:1000}))
  test.table[c(2,3,7), c(2,7,6)] <- NA
  id <- which(is.na(test.table))

    output$table <- renderDataTable(
        datatable(test.table,
                   options = list(drawCallback=JS(
                           paste("function(row, data) {",
                               paste(sapply(1:ncol(test.table),function(i)
                                 paste( "$(this.api().cell(",id %% nrow(test.table)-1,",",trunc(id / nrow(test.table))+1,").node()).css({'background-color': 'lightblue'});")
                                  ),collapse = "\n"),"}" ))
        )))

}
)

如果有人可以帮助我解决这个问题,我将非常高兴

I will be very happy if someone can help me for that problem

问候

Sam

推荐答案

我能够将服务器端处理设置为false。看一下此链接。在1.主题下,开始在主题2之前的最后一段文本。

I was able to make it work with server side processing set to false. Take a look at this link. Under the 1. topic the last piece of text before topic 2. starts.

这是修改后的代码:

shinyApp(
            ui = fluidPage(
                    fluidRow(
                            column(12,
                                   dataTableOutput('table')
                            )
                    )
            ),
            server = function(input, output) {
                    test.table <- data.frame(lapply(1:8, function(x) {1:1000}))
                    test.table[c(2,3,7), c(2,7,6)] <- NA
                    id <- which(is.na(test.table))

                    output$table <- renderDataTable(
                            test.table,
                                      options = list(drawCallback=JS(
                                              paste("function(row, data) {",
                                                    paste(sapply(1:ncol(test.table),function(i)
                                                            paste( "$(this.api().cell(",id %% nrow(test.table)-1,",",trunc(id / nrow(test.table))+1,").node()).css({'background-color': 'lightblue'});")
                                                    ),collapse = "\n"),"}" ))
                                      ), server = FALSE)

            }
    )

这篇关于R Shiny中的DataTable回调行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆