I have about 50 files that I need to call within dataSource1, dataSource2, and dataSource3. Instead of copying it all out into each switch statement, how can I use variables so I only have to type it out once at the top of the code? I am calling this within server.R for a Shiny App.
dataSource1 <- reactive({
switch(input$dataSelection1,
"File1" = File1,
"File2" = File2,
"File3" = File3,
"File50" = File50
)
)
Instead, I would like to have:
dataSource1 <- reactive({
switch(input$dataSelection1,
FileNumber = File
)
)
dataSource2 <- reactive({
switch(input$dataSelection1,
FileNumber = File
)
)
dataSource2 <- reactive({
switch(input$dataSelection1,
FileNumber = File
)
)
You can use the get function that will return the value of a named object. Assuming that input$dataSelection1 has the complete name of the variable, your reactive function could be something like this:
dataSource1 <- reactive({
get(input$dataSelection1)
)
It will return the content of the variable that match the name.
If input$dataSelection1 only contains a number, you can use the paste0 or sprintf functions to build the name of the variable:
get(paste0('File',input$dataSelection1)) # it will create File1, File2...
sprintf('File%04d',input$dataSelection1) # add zeros before the number File0001
I hope it can help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With