【问题标题】:Bookmark state of an R Shiny app with dynamically rendered input具有动态呈现输入的 R Shiny 应用程序的书签状态
【发布时间】:2018-10-28 02:59:21
【问题描述】:

我想为闪亮应用的状态添加书签,其中一个输入字段是通过renderUI() 动态生成的。

所有输入都正确导出到生成的 URL,但是,通过书签调用应用程序时,总是在动态呈现的输入字段中选择第一个条目,而不是存储在 URL 中的那个。 这似乎是由于 reactive() 函数的错误应用,其中调用了独立和相关输入值。 reactive() 函数似乎被调用了两次,一次是在生成依赖输入之前,另一次是之后。我认为如果通过书签调用应用程序,则因变量的值将被忽略,因为尚未定义相应的输入字段。相反,总是选择第一个条目。

我做错了什么,感谢您的澄清。

这是一个可重现的例子:

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)

【问题讨论】:

  • 你用的是开发版的shiny吗?最近在github.com/rstudio/shiny/pull/2139 中修复了书签和reńderUI 的错误
  • 感谢@GregordeCillia 的提示!我阅读了最新闪亮版本的更改日志,并找到了 renderUI 和书签的修复。但是,这两个修复似乎与我的问题无关,并且彼此独立。
  • 有趣。我无法用闪亮的开发版本重现这一点。我链接的问题与 release 版本(1.1.0)无关,但与可以使用devtools::install_github("rstudio/shiny") 安装的 development 版本(1.1.0.9001)有关。跨度>
  • 谢谢@GregordeCillia,我会试试的。
  • 现在工作!谢谢@GregordeCillia

标签: r shiny


【解决方案1】:

您描述的问题已经在闪亮的开发版本中得到修复。

  • 可以在here 找到包含可重现示例的 GitHub 问题。
  • 修复该问题的相应拉取请求是this

但是请注意,此修复是在最后一个 CRAN 版本(版本 1.1.0)之后进行的。因此,您使用install.packages 获得的版本将不包含此修复程序。相反,您必须直接从 GitHub 安装开发版本。这可以通过

devtools::install_github("rstudio/shiny")

【讨论】:

    猜你喜欢
    • 2019-02-05
    • 2018-11-28
    • 2016-10-11
    • 2017-11-30
    • 2016-10-02
    • 1970-01-01
    • 2019-03-04
    • 2017-02-27
    • 2020-11-20
    相关资源
    最近更新 更多