【问题标题】:Passing input$ value to JS() statement in shiny data table将 input$ 值传递给闪亮数据表中的 JS() 语句
【发布时间】:2016-05-02 07:41:39
【问题描述】:

使用此代码格式化我的数据表中的行

 rowCallback = DT::JS(
      'function(row, data) {
        // Bold cells for those >= 5 in the first column
        if (parseFloat(data[0]) >= 5.0)
          $("td", row).css("background", "red");
      }'
    )

我想更改此代码,以便突出显示基于 input$ 值而不是静态“5.0”值。这样用户就可以点击图表上的一个点,具有该值的行将在数据表中突出显示。

但是用 input$click 代替 5 似乎不起作用。想法?

rowCallback = DT::JS(
          'function(row, data) {
            // Bold cells for those >= 5 in the first column
            if (parseFloat(data[0]) >= input$click)
              $("td", row).css("background", "red");
          }'
        )

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    使用最新版本的 DT,您无需任何 Javascript 即可使用formatStyle

    这是一个例子:

    library(shiny)
    library(DT)
    shinyApp(
            ui = fluidPage(numericInput("cutoff", "Test", 5, min = 0, max = 10, step = 1),
                           DT::dataTableOutput('tbl')
            ),
            server = function(input, output) {
                    output$tbl = DT::renderDataTable(
                            datatable(iris, options = list(lengthChange = FALSE)) %>% formatStyle(
                                    'Sepal.Length',
                                    target = 'row',
                                    backgroundColor = styleInterval(input$cutoff, c('gray', 'yellow'))
                            )
                    )
            }
    )
    

    更多信息和示例herehere

    您可能需要通过运行以下命令安装 DT 的开发版本:

    devtools::install_github('rstudio/DT')
    

    如果你不能使用开发版的DT,这里还有一个解决办法:

    library(shiny)
    library(DT)
    shinyApp(
            ui = fluidPage(numericInput("cutoff", "Test", 5, min = 0, max = 10, step = 1),
                           uiOutput("tbl_holder")
    
            ),
            server = function(input, output) {
                    output$tbl_holder <- renderUI({
                            DT::dataTableOutput('tbl')
                    })
    
                    output$tbl = DT::renderDataTable(
                            datatable(iris, options = list(lengthChange = FALSE,rowCallback = DT::JS(
                                    paste0('function(row, data) {
                                    // Bold cells for those >= 5 in the first column
                                    if (parseFloat(data[0]) >=',input$cutoff,')
                                    $("td", row).css("background", "red");
            }')
            )))) 
            }
    )
    

    您可以使用paste在JS函数中添加cutoff,renderUi/uiOutput这样打印数据表的函数会在每次cutoff改变时更新。

    【讨论】:

    • 我已经看到了该功能,但我无法访问 DT 的 devtools 版本。它一直告诉我它无法连接到服务器。然后我尝试直接下载它并从本地文件安装if,它一直在崩溃R,导致内存问题,所以我回到了DT的常规版本,直到他们解决了这个问题。如果不使用开发版本就无法完成,我猜我将不得不添加第二个条件表,但对于一个简单的突出显示问题来说,这似乎是浪费空间。
    • 好吧,这很奇怪,如果你不能使用开发版本,我添加了另一个解决方案。
    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多