I'm trying to save shinyWidgets::virtualSelectInput()
to csv and then load the csv file and update the selected input using shinyWidgets::updateVirtualSelect().
More specifically, I would like the user to pick the input, write the input into a csv file with a user-given name and then choose the save file and load the input saved in it.
However, I get the following errors:
Warning: Error in if: argument is of length zero. [No stack trace available]
and the app crashes.
Here's a reproducible simple example using LETTERS:
ui <- shiny::fluidPage(
shinyWidgets::virtualSelectInput(
inputId = "letters",
label = "Letters",
choices = NULL,
selected = NULL,
multiple = TRUE,
search = T,
placeholder = "Select Letters"
),
shiny::inputPanel(
shiny::textInput(
inputId = "savefilename",
label = "File Name",
placeholder = "Name to save file"),
shiny::actionButton(
"saveFile", "Save"
),
shiny::selectInput(
'selectfile','Select File',
choice = list.files(pattern = ".csv")
),
shiny::actionButton(
"loadFile","Load",
)
),
)
server <- function(input,output,session){
observe(
{
shinyWidgets::updateVirtualSelect(
session = session,
inputId = "letters",
label = "Letters",
choices = LETTERS
)
}
)
# Save File
observeEvent(
input$saveFile,
{
LETTERS_table = tibble("LETTERS_picked" = input$letters) %>%
write_csv(file = paste(paste(input$savefilename,"_.csv",sep = ""),sep = ""))
updateSelectInput(
session = session,
inputId = "selectfile",
choices = list.files(pattern = ".csv")
)
}
)
# Load File
observeEvent(
input$loadFile,
{
LETTERS_table = read_csv(file = input$selectfile)
shinyWidgets::updateVirtualSelect(
session,
inputId = "letters",
choices = LETTERS,
selected = as.character(na.omit(LETTERS_table$LETTERS_picked))
)
}
)
}
shinyApp(ui, server)
For technical reasons - I cannot use shinyWidgets::pickerInput
What's going on? and how can I fix this? Thanks.