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

时间:2016-03-24 14:55:42

标签: r shiny dplyr

我想在我的闪亮应用中使用反应数据集,以便可以根据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)

来做到这一点
Listening on http://127.0.0.1:7032
Warning: Error in UseMethod: no applicable method for 'xtable' 
applied to an object of class "reactive"

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

tableOutput()

并且不显示<Image x:Name="image" HorizontalAlignment="Left" Height="27" Margin="157,257,0,0" VerticalAlignment="Top" Width="26" Source="img/info.png" Cursor="Hand"> <Image.OpacityMask> <ImageBrush ImageSource="img/info.png"/> </Image.OpacityMask> </Image>

1 个答案:

答案 0 :(得分:4)

反应是一种功能......所以你需要parens ......

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)