0
votes

I'm populating a selectInput with updateSelectInput using the file names from a directory. The user selects which directory to populate from using a radioButtons input. Everything works, but when the app is first loaded or the directory is changed, the selectInput reactive passes either the default value or the last selected file from the old directory (for startup and directory change, respectively). This results in a failed load of the data file until the selectInput updates.

How can I get my file loading reactive to wait until the selectInput has been updated?

Here's the relevant code...

ui.R:

radioButtons("system", label = h3("System"),
             choices = list("USAMS" = usamspath, "CFAMS" = cfamspath),
             selected = usamspath),
selectInput("wheelSelect",
            label = h3("Wheel"),
            c("label 1" = "option1")),

server.R:

observe({
  #Get and order wheelnames
  details <- file.info(list.files(path = input$system,
                       pattern = "*AMS*.*", full.names=TRUE))
  details <- details[with(details, order(as.POSIXct(mtime))), ]
  wheels <- basename(rownames(details))

  # Change values for input$wheelSelect
  updateSelectInput(session, "wheelSelect",
                    choices = wheels,
                    selected = tail(wheels, n = 1))
  })

wheelData <- reactive({
  #Code to reload wheel when file changes
  wheelFile <- reactiveFileReader(1000, session,
                 paste(input$system, input$wheelSelect, sep = "/"),
                 read.delim, skip = 4, comment.char = "=")
  z <- wheelFile()
  mungeCFWheel(z)
})

The problem is that input$wheelSelect gets read by the wheelData reactive before it gets updated by updateSelectInput in the preceeding observe().

1

1 Answers

1
votes

Finally figured this out. I don't know if it's the correct fix or a hack, but using the shiny function validate() to check whether the filename generated by the selectInput is valid seems to work.

Adding this to the wheelData reactive function does the trick:

validate(
  need(file.exists(
    paste(input$system, input$wheelSelect, sep = "/")),
  message = FALSE)
)

Making the message = FALSE allows it to fail silently until the selectInput generates a valid filename.