If user selects dataset1, load files A, B, and C - but if user selects dataset2, load files D, E, F ?

I have the following CSV files in my working directory:

  • DATASET1.csv
  • DATASET2.csv
  • A.csv
  • B.csv
  • C.csv
  • D.csv
  • E.csv
  • F.csv

I want a user to be able to choose in a dropdown menu to load DATASET1, or DATASET2. This works for me into what I will call myCSV. This has been working for me :

selectInput("TheFile", "Select dataset", 
                                        choices = c("DATASET1.csv", "DATASET2.csv"))

 myCSV <- reactive({
    read.csv(input$TheFile)
  })

How do I link DATASET1 to files A, B, and C - to read them also? But if a user were to select DATASET2, to then read in B,C, and D?

Could you have code like this

OtherData <- reactive({
  OutList <- vector(mode = "list", length = 3)
  if(input$TheFile == "DATASET1.csv") {
    OutList[[1]] <- read.csv("A.csv")
    OutList[[2]] <- read.csv("B.csv")
    OutList[[3]] <- read.csv("C.csv")
  } else {
      OutList[[1]] <- read.csv("D.csv")
      OutList[[2]] <- read.csv("E.csv")
      OutList[[3]] <- read.csv("F.csv")
  }
  OutLIst
})

You can then refer to OtherData()[[1]] to get either A.csv or D.csv

This is great but I'm not having much luck implementing it (trying to insert it into my code - I posed it as another thread but didn't get any response).

Is there any way that instead of 'if not DATASET1, load D,E,F instead that it could be more like "If DATASET2, load DEF) ? I'm wondering because I eventually would like to insert 10+ choices and not only 2.

Thanks for your help with this!

This toy example works for me. It does not even try to load the files DATASET1.csv or DATASET2.csv but it does jump between loading the files A.csv, B.csv, etc.

library(shiny)

ui <- pageWithSidebar(
  headerPanel("My First Shiny App"),
  
  sidebarPanel( 
    selectInput("TheFile", "Select dataset", 
                choices = c("DATASET1.csv", "DATASET2.csv"))
  ),     
  mainPanel(
    tableOutput("Table1"),
    tableOutput("Table2"),
    tableOutput("Table3")
  )
)

server <- function(input, output, session){
  OtherData <- reactive({
    OutList <- vector(mode = "list", length = 3)
    if(input$TheFile == "DATASET1.csv") {
      OutList[[1]] <- read.csv("A.csv")
      OutList[[2]] <- read.csv("B.csv")
      OutList[[3]] <- read.csv("C.csv")
    } else {
      OutList[[1]] <- read.csv("D.csv")
      OutList[[2]] <- read.csv("E.csv")
      OutList[[3]] <- read.csv("F.csv")
    }
    OutList
  })
  
  output$Table1 <-renderTable({
    OtherData()[[1]]
  })
  output$Table2 <-renderTable({
    OtherData()[[2]]
  })
  output$Table3 <-renderTable({
    OtherData()[[3]]
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

I do not understand what you are referring to with "Is there any way that instead of 'if not DATASET1, load D,E,F...". My first code loaded A, B and C if the input was selected as DATASET1.csv. If you have several such options you can use a set of

if () {
} else if {
} else if {
} else {
}

to handle them all.

If your file names are as regular in your actual use case as they are in your example (or can be made regular), I think you can select the appropriate files as follows: In the code below, we get the number at the end of the DATASET name and feed that to a function that maps that number to the three unique letter files that go with it. The we read those three letter files:

library(tidyverse)

# Function to identify and read the desired LETTER files for a given DATASET
rf = function(dataset.num) {
  n = (dataset.num - 1) * 3 + 1
  files = paste0(LETTERS[n:(n+2)], ".csv")
  map(files, read_csv)
}

# Get number of DATASET
num = as.numeric(stringr::str_extract(input$TheFile, "[0-9]"))

# Read the appropriate letter files
OutList = rf(num)

Since there are only 26 letters, this particular mapping would work for 8 different DATASET files. But you could call your three target files for DATASET1 1a.csv, 1b.csv, 1c.csv, and likewise for DATASET2, etc., and the code above could be adjusted accordingly.

2 Likes

FJCC,

Thanks very much for teaching about this. It works beautifully and I'm implementing it into my app.

Many thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.