Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dataset reactive in Shiny?

Tags:

r

dplyr

shiny

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.

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"

And tableOutput() is not displayed.

like image 437
Dambo Avatar asked Mar 24 '16 14:03

Dambo


People also ask

How does reactive work in Shiny?

A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. To create a reactive expression use the reactive function, which takes an R expression surrounded by braces (just like the render* functions).

What is a reactive value Shiny?

Description. The reactiveVal function is used to construct a "reactive value" object. This is an object used for reading and writing a value, like a variable, but with special capabilities for reactive programming.

How do you load data on the Shiny app?

Data is loaded into shinyapps.io in a few different ways: The simplest way to get data into an application is by uploading a CSV, RData or other data file directly with the application source code. In this model, the application author includes the data files as part of the application.

What does the renderPlot function do in Shiny?

renderPlot is an reactive function that can take input data from the ui. R script and feed it into the server. R script. It then actively updates the information within its function.


1 Answers

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)
like image 157
cory Avatar answered Sep 25 '22 12:09

cory