input - Avoid re-loading datasets within a reactive in shiny -


i have shiny app requires input 1 of several files. simplified example be:

library(shiny) x <- matrix(rnorm(20), ncol=2) y <- matrix(rnorm(10), ncol=4) write.csv(x, 'test_x.csv') write.csv(y, 'test_y.csv') runapp(list(ui = fluidpage(    titlepanel("choose dataset"),    sidebarlayout(       sidebarpanel(           selectinput("data", "dataset", c("x", "y"), selected="x")           ),       mainpanel(           tableoutput('contents')       )    ) ) , server = function(input, output, session){ mydata <- reactive({     infile <- paste0("test_", input$data, ".csv")     data <- read.csv(infile, header=false)     data })     output$contents <- rendertable({          mydata() })  })) 

in reality, files read in large, avoid reading them in each time input$data changes, if has been done once. example, making matrices mat_x , mat_y available within environment, , within mydata testing:

if (!exists(paste0("mat_", input$data))) {     infile <- paste0("test_", input$data, ".csv")     data <- read.csv(infile, header=false)     assign(paste0("mat_", input$data), data) } 

is there way this, or have create separate reactive mat_x , mat_y , using within mydata? have 9 possible input files, each user may want use 1 or two.

you

mydata <- reactive({ data <- fetch_data(input$data) data )}  fetch_data <- function(input) {     if (!exists(paste0("mat_", input))) {        infile <- paste0("test_", input, ".csv")        data <- read.csv(infile, header=false)        assign(paste0("mat_", input), data)     } else {        data <- paste0("mat_", input)     }      return (data) } 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -