【发布时间】: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