This is my first time posting on stackoverflow, despite being a user for some time, so I will do my best to be clear.
I have an application that has four select inputs that each inter-relate with each other (dependent on the selection made in the first input, the list of options in the second input list updates, and so on). I want users of my application to be able to copy the URL generated by their selections to return to the application at a later date if they wish, returning to find the application the same way the left it (including the selections made).
So far I can make this work for the first two inputs, meaning the URL generated populates these two inputs correctly, however, selections saved in the third and fourth inputs do not update as expected. I think this is because of the reactive elements of my application are triggered by the activation of the URL but I do not know how to over come this problem (and I am tearing my hair out at this point).
I have tried looking at other posts, including the following examples, but was not able to implement the solution as outlined:
Shiny bookmark cannot restore the selectizeinput
Shiny App re-runs observeEvent when restoring a bookmarked state
I provide a minimal example below.
library(shiny)
library(tidyverse)
cut <- diamonds %>%
distinct(cut)
ui <-
function(request){fluidPage(sidebarPanel(
id = "sidebar-diamonds",
selectInput(inputId = "cut", label = "Cut", choices = cut, selected = "Ideal"),
selectInput(inputId = "carat", label = "Carat", choices = ""),
selectInput(inputId = "color", label = "Color", choices = ""),
selectInput(inputId = "clarity", label = "Clarity", choices = ""),
uiOutput("cut"),
uiOutput("carat"),
uiOutput("color"),
uiOutput("clarity"),
width = 3),
mainPanel("Text",
htmlOutput("string",height = 320)
)
)}
server = function(input, output, session) {
# Automatically bookmark every time an input changes
observe({
reactiveValuesToList(input)
session$doBookmark()
})
# Update the query string
onBookmarked(updateQueryString)
#Update carat list based on cut
observe({
updateSelectInput(session,
"carat",
choices = unique(diamonds[diamonds$cut == input$cut, "carat"]))
})
#Update color list
observe({
updateSelectInput(session,
"color",
choices = unique(
diamonds[diamonds$cut == input$cut &
diamonds$carat == input$carat,
"color"]))
})
#Update clarity list
observe({
updateSelectInput(session,
"clarity",
choices = unique(
diamonds[diamonds$cut == input$cut &
diamonds$carat == input$carat &
diamonds$color == input$color,
"clarity"]))
})
## Update inputs directly from URL when required
onRestored(function(state) {
updateSelectInput(session, "cut", selected = state$input$cut)
updateSelectInput(session, "carat", selected = state$input$carat)
updateSelectInput(session, "color", selected = state$input$color)
updateSelectInput(session, "clarity", selected = state$input$clarity)
})
output$string <- renderUI({
HTML(paste(input$cut,input$carat,input$color, input$clarity, sep = "-"))
})
}
shinyApp(ui, server, enableBookmarking = "url")