Help in selecting appropriate csv files according to user input


I have a drop down list of book names in which user can choose the required book title. The code for it in ui.R looks like

selectInput('x', label = 'Choose the book', choices = c('book1_name',
                selected = 'book1_name')

In my application folder, I have two csv files for each book. I want to read the corresponding two csv files in two variables according to the user choice from the drop down menu. Something like:

data1<- read.csv('1_1.csv')
data2 <- read.csv('1_2.csv')

And then use data1 and data2 for further computations.
What should be my code in server.R
I tried using eventReactive but cannot manage to get the right script. Kindly help!



If you want them to be two separate objects to use later than you could either put them in one reactive expression that returns a vector and access them like you would any other vector or create a reactive expressions for each one (my prefered way, just for clarity when someone else, or a later version of myself, has to look at the code and figure out what is going on). This is assuming that the rest of the books follow the same naming convention as 1_1.csv:

data_1 <- reactive({
  if (input$x == "book1_name") {
    # what you name the variable in here does not matter so long as you
    # return it at the end of the reactive statement 
    # this is the case because reactive statements are functions that behave a certain way
    out <- read.csv("1_1.csv")
  } else if (input$x == "book2_name") {
    out <- read.csv("2_1.csv")
  } else out <- read.csv("3_1.csv") # assuming you actually only have three options

You would then be able to access this variable in the rest of your server code as data_1(). You need to add the parentheses at the end becasue, as I mentioned in the comments, a reactive expression is a function. You can create data_2 in the exact same way.

FFR, it is much easier to give help if you post a minimal reproducible example. You question is fairly straight forward, assuming I understand it correctly, but posting the code that you have already tried and the errors that you are getting can help you get the answer you are looking for faster. You should check out the reprex package to help you make your examples.