While creating a form with CRUD functionality in my Shiny app, I encountered a situation that when I used the updatePickerInput
function it would not work correctly when selecting the first input.
I have created a reproducible sample app that shows this problem. The first code block is the app that does work correct (using the second selected item in the named list) and the second has the first named item in the list and this does not work correctly. The difference between the two apps is in the line where the variable "selected_var" is defined (end of code block).
To reproduce the problem, you need to take the following steps:
- Select 'Asset Type #1' in the dropdown
- click the button.
- The correct result is that it should update the selected item in the picker. In the first example this is the 'Asset Type #2' and in the second this should be 'Asset Type #1'.
It is working for the #2 but not for #1.
Any ideas on how to resolve this issue?
Example that works correct:
library(shiny)
library(shinyWidgets)
# Define options
# Define UI
ui <- fluidPage(
uiOutput("pickerInput"),
actionButton(inputId = "update_button", label = "Update selected option")
)
# Define server
server <- function(input, output, session) {
options_tbl <- structure(
list(
transportasset_id = c(
"a40cf288-d3c0-11ed-8b73-2e080658eb35",
"b48abb72-d3c0-11ed-8b73-2e080658eb35"
),
transportasset_name = c(
"Asset Type #1 ",
"Asset Type #2"
)
),
row.names = c(NA, -2L),
class = c(
"tbl_df",
"tbl",
"data.frame"
)
)
var_options <- setNames(options_tbl$transportasset_id, options_tbl$transportasset_name)
output$pickerInput <- renderUI(
pickerInput(inputId = "my_picker", label = "Select an option:",
choices = var_options,
selected = NULL,
options = pickerOptions(
liveSearch = TRUE,
noneSelectedText = "No commodity selected",
size = "auto",
title = "Select commodity"
)
),
)
# Function to update picker input
observeEvent(input$update_button, {
options_tbl %>% print()
selected_var <- options_tbl %>% filter(transportasset_id == "b48abb72-d3c0-11ed-8b73-2e080658eb35") %>% pull(transportasset_id)
updatePickerInput(session, "my_picker", selected = selected_var)
})
}
# Run app
shinyApp(ui = ui, server = server)
Example that does not work correct:
library(shiny)
library(shinyWidgets)
# Define options
# Define UI
ui <- fluidPage(
uiOutput("pickerInput"),
actionButton(inputId = "update_button", label = "Update selected option")
)
# Define server
server <- function(input, output, session) {
options_tbl <- structure(
list(
transportasset_id = c(
"a40cf288-d3c0-11ed-8b73-2e080658eb35",
"b48abb72-d3c0-11ed-8b73-2e080658eb35"
),
transportasset_name = c(
"Asset Type #1 ",
"Asset Type #2"
)
),
row.names = c(NA, -2L),
class = c(
"tbl_df",
"tbl",
"data.frame"
)
)
var_options <- setNames(options_tbl$transportasset_id, options_tbl$transportasset_name)
output$pickerInput <- renderUI(
pickerInput(inputId = "my_picker", label = "Select an option:",
choices = var_options,
selected = NULL,
options = pickerOptions(
liveSearch = TRUE,
noneSelectedText = "No commodity selected",
size = "auto",
title = "Select commodity"
)
),
)
# Function to update picker input
observeEvent(input$update_button, {
options_tbl %>% print()
selected_var <- options_tbl %>% filter(transportasset_id == "a40cf288-d3c0-11ed-8b73-2e080658eb35") %>% pull(transportasset_id)
updatePickerInput(session, "my_picker", selected = selected_var)
})
}
# Run app
shinyApp(ui = ui, server = server)