Shiny App crashes when a non-exisiting directory is chosen

150 Views Asked by At

My shiny App lets the User choose some Patterns (e.g. company Name) which are part of the path Name like:

  Dir <- paste(x,"/",input$Company, "/", input$folder,  " Selection_ESG_Files/", sep="")
  
  dtVaR <- read_csv(paste(Dir, input$Candidate, '-VaR.csv', sep = '')) # load VaR.csv

However, if I choose an Input which ends up in a path that does not exist the App crashes. I would like to display an error message that a wrong path was selected and that a new one can be re-selected without the App crashing. Is there a potential way with the validate function?

1

There are 1 best solutions below

4
On

You can do something like that:

Dir <- reactive({
  paste0(x, "/", input$Company, "/", input$folder, " Selection_ESG_Files/")
})

dtVaR <- reactive({
  req(Dir())
  File <- paste0(Dir(), input$Candidate, '-VaR.csv')
  if(file.exists(File)){
    return(read.csv(File))
  }else{
    showNotification("Invalid file.")
  }
})