在 shiny 中使用过滤后的数据表

Using filtered datatables in shiny

我是 shiny 的新手,但想知道是否有任何方法可以在 R 中存储 过滤数据表 (使用列过滤器)对象,以便可以将过滤后的数据传递给 直方图 绘图函数 .

编辑 15 年 5 月 7 日:包括作者对评论的扩展解释

I want the table to get filtered using the built-in column filters and then want the plot to automatically adjust. I've already tried the DT package but I don't like very much of the column filters that come with this package as it is not possible (I think) to remove the filters from a subset of the columns in the table

@NicE 建议的 example 非常有帮助。我在下面包括了一个最小的例子:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(dataTableOutput('tbl'),
                 plotOutput('plot1')),
  server = function(input, output) {    
    output$tbl = renderDataTable({
      datatable(iris, options = list(lengthChange = FALSE))
    })
    output$plot1 = renderPlot({
      filtered_data <- input$tbl_rows_all
      hist(iris[filtered_data, "Sepal.Length"])
    })
  }
)

这将从 iris 数据集中为 DT::datatable 中过滤后的数据生成 Sepal.Length 的直方图。

注意:这假定以下版本的 DTshiny

DT_0.0.39 shiny_0.11.1.9005

仅在@JasonAizkalns 的示例的基础上,您可以使用 jQuery 隐藏一些内置的列过滤器。例如这里前两个是隐藏的:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(dataTableOutput('tbl'),
                 plotOutput('plot1')),
  server = function(input, output) {    
    output$tbl = renderDataTable({
      datatable(iris, filter="top",options = list(lengthChange = FALSE),callback=JS("
           //hide column filters for the first two columns
          $.each([0, 1], function(i, v) {
                $('input.form-control').eq(v).hide()
              });"))
    })
    output$plot1 = renderPlot({
      filtered_data <- input$tbl_rows_all
      hist(iris[filtered_data, "Sepal.Length"])
    })
  }
)