如何使一个数据集反应在闪亮? [英] How to make a dataset reactive in Shiny?

查看:130
本文介绍了如何使一个数据集反应在闪亮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在闪亮的应用程序中使用一个反应数据集,以便使用该数据集的任何其他对象可以根据 reactiveDf 中的值重新呈现。

I would like to use a reactive dataset in my shiny app, such that any other objects that uses that dataset can be re-rendered according to the values in reactiveDf.

在此示例中,我只输出一个表,但在我的应用程序中,我有其他图表和表,并且想法是通过子集<$ c $来触发呈现c> reactiveDf 。此外,我想使用 dplyr

In this example I am outputting only one table, but in my app I have other charts and tables, and the idea is to trigger the rendering by subsetting reactiveDf only. Also, I would like to do that using 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)

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

Right now, when I run this app I get:

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()不显示。

推荐答案

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

A reactive is a function... so you need 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)

这篇关于如何使一个数据集反应在闪亮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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