Harshita Jhavar
Harshita Jhavar

Reputation: 145

Selecting specific csv files based on user input in shiny web appliction

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’,
‘book2_name’,
‘book3_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:

if(input$x==‘book1_name’){
data1<- read.csv(‘1_1.csv’)
data2 <- read.csv(‘1_2.csv’)
}
else if{
  (input$x==‘book2_name’){
    data1<- read.csv(‘2_1.csv’)
    data2 <- read.csv(‘2_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.

Upvotes: 0

Views: 643

Answers (1)

MLavoie
MLavoie

Reputation: 9836

You did not provide a reproducible example, so instead, I used the mtcars and iris datasets. You can read your own datasets in the global environment. For the sake of the example, I am using the renderPlot function to show you it works. Here are two suggestions:

Option 1:

library(shiny)
library(tidyr)

ui = pageWithSidebar(
  headerPanel('testing'),
  sidebarPanel(
    selectInput('x', label = 'Choose the book', choices = c('book1_name', 'book2_name'), selected = 'book1_name')
  ),
  mainPanel(
    plotOutput('plot1')
  )
)

  server = function(input, output) {


    output$plot1 <- renderPlot({


      if (input$x == 'book1_name') {
        data1 <- iris
        data2 <- mtcars
        rownames(data2) <- NULL

        data1a <- data1[, 1:2]
        data2a <- data2[, 1:2]

        par(mfrow = c(1, 2)) 
        plot(data1a) #
        plot(data2a)  #
      } 

      if (input$x == 'book2_name') {
        data1 <- mtcars
        data2 <- iris
       # rownames(data2) <- NULL

        data1a <- data1[, 1:2]
        data2a <- data2[, 1:2]

        par(mfrow = c(1, 2)) 
        plot(data1a) #
        plot(data2a)  #
      } 


    })

  }

  shinyApp(ui = ui, server = server)

With option 1, you can pick your datasets within the render function (it could be another computation).

Option 2:

library(shiny)

ui = pageWithSidebar(
  headerPanel('testing'),
  sidebarPanel(
    selectInput('x', label = 'Choose the book', choices = c('book1_name', 'book2_name'), selected = 'book1_name')
  ),
  mainPanel(
    plotOutput('plot1')
  )
)

server = function(input, output) {


  data1 <- reactive({
    if (input$x == 'book1_name') {
      data1 <- iris
    } else {
      data1 <- mtcars
    }
  })

  data2 <- reactive({
    if (input$x == 'book1_name') {
      data2 <- mtcars
    } else {
      data1 <- iris
    }
  })


  output$plot1 <- renderPlot({

    data1 <- data1()
    data2 <- data2()

      data1a <- data1[, 1:2]
      data2a <- data2[, 1:2]

      par(mfrow = c(1, 2)) 
      plot(data1a) #
      plot(data2a)  #


  })

}

shinyApp(ui = ui, server = server)

With option 2, you can pick the datasets outside the render function.

Hope it helps!

Upvotes: 2

Related Questions