Home > Blockchain >  Filter through CSV files based on name and updateselectizeInput on R shiny
Filter through CSV files based on name and updateselectizeInput on R shiny

Time:01-20

I am new to RShiny (and R in general) and cannot quite figure out how to do the following thing. I have a list of 50 CSV files ending in either P2.csv,P3.csv or P5.csv. I would like to make an app that would allow the user to select a specific price (P2,P3,P5) and then present them with the list of csv files with that ending pattern out of which the user could then select maximum 3 that would need to be loaded into memory.

I have found separately how to list all the files with the specific ending and I figured that I would need to use updateSelectizeInput so that each time the user selects a price (e.g. P2) he gets presented with all the csv files appleP2.csv, orangeP2.csv etc for example, but cannot think of a way to make this work.

Any help is greatly appreciated! Thank you.

library(shiny)

#listing the csv files ending with a same pattern 
same_price_P2<-list.files(pattern="P2.csv")
same_price_P3<-list.files(pattern="P3.csv")
same_price_P5<-list.files(pattern="P5.csv")

shinyUI(fluidPage(
  
  titlePanel("Price of fruit"),
  
  #user can select the price of the fruit
  selectInput("Price", "Price", choices = c("5 euros"="P5", "2 euros"="P2", "3 euros"="P3"), selected="P2", multiple=FALSE)),
  
  #user must be presented only with the CSV files associated to the selected price and can select maximum of 3 
  #files to be read 
  selectizeInput("Fruit", "Fruit", choices = "", selected = NULL, multiple=TRUE, options=list(maxItems=3)),
  

  
  tableOutput("dataset")
)


shinyServer(function(session, input, output){
  
  
  #Populate this by filtering through and showing the csv files based on whether they end in P2.csv, P5.csv, P3.csv
  #the selected files by the user should then be read
  observeEvent(
    input$Price,
    updateSelectizeInput(session, "Fruit", "Fruit", 
                      choices = ................[..........==input$Price]))
  
 
  output$dataset <- renderTable({
    data
  })
  
})

CodePudding user response:

You already found the main steps.

The last steps are:

  1. Insert all possible csv names (regardless of the ending) in the choice argumente of the selectizeInput function. (We need this vector for the second step too, therefore it is specified outside of the selectizeInput function. -> all_price)
  2. Filter the choices by the input$Price variable inside the updateSelectizeInput.
  3. Load tables into memory.

The code below will help you with the first to steps. Because that seems to be the issue you are currently working on.

library(shiny)

#listing the csv files ending with a same pattern 
same_price_P2 <- c("appleP2.csv",  "bananaP2.csv", "cranberryP2.csv", "dateP2.csv")
same_price_P3 <- c("appleP3.csv",  "bananaP3.csv", "cranberryP3.csv", "dateP3.csv")
same_price_P5 <- c("appleP5.csv",  "bananaP5.csv", "cranberryP5.csv", "dateP5.csv")
all_price <- c(same_price_P2, same_price_P3, same_price_P5)

ui <- fluidPage(
  
  titlePanel("Price of fruit"),
  
  #user can select the price of the fruit
  selectInput("Price",
              "Price",
              choices = c("5 euros" = "P5", 
                          "2 euros" = "P2",
                          "3 euros" = "P3"),
              selected = "P2",
              multiple = FALSE),
  
  #user must be presented only with the CSV files associated to the selected price and can select maximum of 3 
  #files to be read 
  selectizeInput("Fruit",
                 "Fruit", 
                 choices = all_price, 
                 selected = NULL, 
                 multiple =  TRUE,
                 options = list(maxItems = 3)),
  
  
  textOutput("dataset")
)


server <- function(input, output, session){
  
  
  #Populate this by filtering through and showing the csv files based on whether they end in P2.csv, P5.csv, P3.csv
  #the selected files by the user should then be read
  observe({
    updateSelectizeInput(session, 
                         "Fruit", 
                         "Fruit", 
                         choices = grep(paste0(input$Price,
                                               ".csv$"),
                                        all_price,
                                        value = T))
    })
  
  
  output$dataset <- renderText({
    input$Fruit
  })
  
}

shinyApp(ui, server)

  •  Tags:  
  • Related