0

I have a shiny App to create various plots from columns in a dataframe. My idea is to let the user select which column he'd like to plot with a select input, but I don't want to bake the options into the selectInput, instead, I'd like to have it update once the source file has been loaded.

I've been trying to use an observing function but my app crashes every time I open it, with not so much as a clear error message.

I have a function in my server which takes the input file and returns it as a data frame.

loadData <- reactive({
        req(input$file1)
        
        tryCatch(
            {
                df <- fread(check.names = FALSE, input$file1$datapath,
                            header = TRUE,
                            sep = input$sep,
                            quote = input$quote,
                            na.strings=c(""))
            },
            error = function(e) {
                # return a safeError if a parsing error occurs
                print(e)
                stop(safeError(e))
            }
        )
        
    })

And I was trying to update the choices with this

output$updateCols <- observe({
        req(input$file1)
        
        tryCatch(
            {
                cols <- input$colNames
                df <- loadData()
                newCols <- colnames(df)
                if(is.null(cols) & !is.null(df))
                updateSelectInput(inputId = 'colNames', label = 'Select Column to plot', 
                                  choices = unique(newCols))
                
            },
            error = function(e) {
                print(e)
                stop(safeError(e))
            }
        )
    })

I've searched numerous other examples but haven't found out that works (I pray to see the day where R adopts a single coding standard so that we don't have to juggle through 300 different syntax to do anything)

ThePorcius
  • 123
  • 8
  • The first argument is `session`, not the input id. Try `updateSelectInput(session, inputId = 'colNames', label = ...)`. (`session` is defined in `server <- function(input, output, session) {...}`.) – r2evans Jan 04 '21 at 01:45
  • You seem to be missing `session` in your call to `updateSelectInput`. Why do you have `cols <- input$colNames`? You don't seem to be using that value. – MrFlick Jan 04 '21 at 01:46
  • I forgot to add the session but it's not part of my problem. The code with session is still not updating the input. – ThePorcius Jan 04 '21 at 01:47
  • All these `tryCatch` blocks may be hiding the true error. Are they really necessary? Please try to make a runnable [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can really see what you are doing. – MrFlick Jan 04 '21 at 01:49

0 Answers0