将input $值传递给闪亮数据表中的JS()语句 [英] Passing input$ value to JS() statement in shiny data table

查看:65
本文介绍了将input $值传递给闪亮数据表中的JS()语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using this code to format the rows in my datatable

 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");
      }'
    )

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

I would like to alter this code so that rather than the static "5.0" value, highlighting is based on an an input$ value. So that users could click on a point on the chart, and rows with that value would be highlighted in the data table.

但是用input $ click代替5不会似乎有效。有想法吗?

But substituting input$click for 5 doesn't seem to work. Thoughts?

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");
          }'
        )


推荐答案

使用DT的最新版本,您可以使用 formatStyle 来执行此操作,而无需任何Javascript。

Using the newest version of DT, you can do this without any Javascript using 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'))
                        )
                )
        }
)

更多信息和示例此处此处

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

You will probably need to install the development version of DT by running:

devtools::install_github('rstudio/DT')

如果您不能使用DT的开发版本,这是另一种解决方案:

If you can't use the dev version of DT, here's another solution:

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");
        }')
        )))) 
        }
)

您可以使用粘贴在JS函数和 renderUi / uiOutput ,以便每次更改截止值时都会更新打印数据表的功能。

You can use paste to add the cutoff in the JS function and renderUi/uiOutput so that the function that prints the datatable is updated each time the cutoff changes.

这篇关于将input $值传递给闪亮数据表中的JS()语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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