如何在 Shiny 中使数据集具有反应性?

Dam*_*mbo 5 r shiny dplyr

我想在我闪亮的应用程序中使用反应性数据集,这样使用该数据集的任何其他对象都可以根据reactiveDf.

在这个例子中,我只输出一个表格,但在我的应用程序中我有其他图表和表格,这个想法是reactiveDf仅通过子集触发渲染。另外,我想使用dplyr.

library(shiny)
library(dplyr)
ui <- shinyUI(fluidPage(
   sidebarLayout(
      sidebarPanel(
        checkboxGroupInput('Category', '',
                           unique(mtcars$carb), selected = unique(mtcars$carb))),
      # Show table of the rendered dataset
      mainPanel(
         tableOutput("df")
      )
   )
))


server <- shinyServer(function(input, output) {

  reactiveDf <- reactive({tbl_df(mtcars) %>% 
                                  filter(carb %in% input$Category)})

   output$df <- renderTable({reactiveDf})
})

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

现在,当我运行这个应用程序时,我得到:

Listening on http://127.0.0.1:7032
Warning: Error in UseMethod: no applicable method for 'xtable' 
applied to an object of class "reactive"
Run Code Online (Sandbox Code Playgroud)

并且tableOutput()不显示。

cor*_*ory 5

反应式是一个函数……所以你需要括号……

library(shiny)
library(dplyr)
ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput('Category', '',
                         unique(mtcars$carb), selected = unique(mtcars$carb))),
    # Show table of the rendered dataset
    mainPanel(
      tableOutput("df")
    )
  )
))


server <- shinyServer(function(input, output) {

  reactiveDf <- reactive({return(tbl_df(mtcars) %>% 
      filter(carb %in% input$Category))})

  output$df <- renderTable({reactiveDf()})
})

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)