Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny, load data based on input

Tags:

r

shiny

I have data for each day in server side and want to load data based on date input

On server side, I have this:

 dateInput("date","Enter a date:",value = "2014-01-13"))

On UI side,

library(shiny)
library(googleVis)
library(rpart.plot)

load("data_2014_01_13_new.RData")  #seg and fit are data in this file
shinyServer(function(input, output) {
    output$values <- renderGvis({
      gvisTable(seg[seg$rate >= input$test[1] & seg$rate <= input$test[2],])
    })
    output$plot <- renderPlot({
     prp(fit,extra=T)
    })
})

I want to put load into server function and can load different data as date changes. Thanks!

like image 425
Yoki Avatar asked Mar 24 '14 14:03

Yoki


1 Answers

Read these pages in the tutorial:

  • http://rstudio.github.io/shiny/tutorial/#scoping
  • http://rstudio.github.io/shiny/tutorial/#reactivity

You can put the load call inside of the shinyServer function in another reactive so that you can reference the dataset dynamically, and each session can have different data loaded simultaneously.

So add a function like this into your shinyServer function (note that you'll probably need to massage the format of the date input string to make it compatible with how your files are named).

dataset <- reactive({
  filename <- paste0("data_", input$date, ".Rdata")
  load(filename)
})

then you can reference dataset() in other places in server.R to get the appropriate value for this user's dataset.

like image 146
Jeff Allen Avatar answered Nov 03 '22 19:11

Jeff Allen