0
votes

I want to bookmark the state of a shiny app in which one input field is dynamically generated via renderUI().

All inputs are correctly exported to the generated URL, however, calling the app via the bookmark, always the first entry is selected in the dynamically rendered input field, not the one stored in the URL. This seems to be due to an incorrect application of the reactive() function, in which both the independent and the dependent input values are called. The reactive() function seems to be called twice, once before the dependent input is generated and a second time afterwards. I think that if the app is called via the bookmark, the value for the dependent variable is ignored because the corresponding input field is not yet defined. Instead, always the first entry is selected.

What am I doing wrong, I'm grateful for clarification.

Here is a reproducible example:

library(shiny)
enableBookmarking(store = "url")

ui <- function(request) {
    fluidPage(
    selectInput("independentInput",
                "A or B",
                choices = c("A", "B"),
                multiple = FALSE),

    uiOutput("dependentInput"), # depends on 'independentInput'

    textOutput("finalOutput"),

    bookmarkButton()
)}

server <- function(input, output) {
    # the choices of the secondary select field depend on the "independentInput" selection
    output$dependentInput <- renderUI({

        if (input$independentInput == "A") {
            .label <- "A's child?"
            .choices <- c("a1", "a2", "a3") # one set of secondary choices
        }

        if (input$independentInput == "B") {
            .label <- "B's child?"
            .choices <- c("b1", "b2", "b3") # an alternative set of secondary choices
        }

        selectInput("dependentInput",
                    label = .label,
                    choices = .choices,
                    multiple = FALSE)
    })

    reac <- reactive({

        foo <- input$independentInput
        bar <- input$dependentInput

        print(paste(foo, bar)) # proves that 'reac' initially runs twice, where 'bar' is NULL during the first run.

        if (foo == "A") {
            return(paste0(foo, "'s child is", bar))
        }

        if (foo == "B") {
            return(paste(foo, "'s child is", bar))
        }
    })

    output$finalOutput <- renderText({
        reac()
    })
}

shinyApp(ui = ui, server = server)
1
Do you use the dev version of shiny? There was a recent bugfix with bookmarks and reńderUI in github.com/rstudio/shiny/pull/2139Gregor de Cillia
Thanks for the hint @GregordeCillia ! I read the change log of the latest shiny release and found fixes for both renderUI and bookmark. However, both fixes seem unrelated to my problem and also independent of one another.Marko Lipka
Interesting. I cannot reproduce this with the development version of shiny. The issue I linked does not relate to the release version (1.1.0) but to the development version (1.1.0.9001) which can be installed with devtools::install_github("rstudio/shiny")Gregor de Cillia
Thank you @GregordeCillia, I will try this.Marko Lipka
Works now! Thank you @GregordeCilliaMarko Lipka

1 Answers

0
votes

The issue you descibe has already been fixed in the development version of shiny.

  • A GitHub issue including a reproducible example can be found here.
  • The corresponding pull request that fixes the issue is this.

Note however, that this fix was made after the last CRAN release (version 1.1.0). Therefore, the version you get with install.packages will not include this fix. Instead, you will have to install the development verison directly from GitHub. This can be done with

devtools::install_github("rstudio/shiny")