Set global object in Shiny

Let's say I have the following server.R file in brilliant form:

shinyServer(function(input, output) { output$plot <- renderPlot({ data2 <- data[data$x == input$z, ] # subsetting large dataframe plot(data2$x, data2$y) }) output$table <- renderTable({ data2 <- data[data$x == input$z, ] # same subset. Oh, boy... summary(data2$x) }) }) 

What can I do to not start data2 <- data[data$x == input$z, ] in every render call? If I do the following, I get an "object of type" closure "not a subset":

 shinyServer(function(input, output) { data2 <- reactive(data[data$x == input$z, ]) output$plot <- renderPlot({ plot(data2$x, data2$y) }) output$table <- renderTable({ data2 <- data[data$x == input$z, ] summary(data2$x) }) }) 

What have I done wrong?

+8
r shiny subset
Jul 16 '13 at 18:08
source share
1 answer

data2 is a function that returns the subset you are looking for. So you need to call data2 and save the output in some variable, then you can build / sum various columns

 ## data should be defined somewhere up here or in global.R shinyServer(function(input, output) { data2 <- reactive(data[data$x == input$z, ]) output$plot <- renderPlot({ newData <- data2() plot(newData$x, newData$y) }) output$table <- renderTable({ newData <- data2() summary(newData$x) }) }) 

If you have not already done so, I recommend reading http://rstudio.imtqy.com/shiny/tutorial/#welcome . The reactivity page solves this issue pretty well.

+17
Jul 16 '13 at 18:27
source share



All Articles