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)