我有一个我需要在observeEvent中途更新的renderUI。
基本上,当我单击下面示例中的加载按钮时,首先输入$ Test1需要使用新值更新,然后我需要更新renderUI,然后我需要更新此renderUI中的输入值。 (这通常来自我加载文件,但这个简化版本就足够了)
我在下面对此进行了简化。基本上,我需要输入$ DynamicTwo才能在第一次传递时填写 - 它目前只在我点击输入$ Load两次后填写。
library(shiny)
ui <- navbarPage(
title = "navbar",
id = "navbar",
tabPanel(
"panel1",
selectizeInput("Test1",label="Test1: UI on panel2 depends on this",choices=c("One","Two","Three"),multiple=T,selected="One"),
actionButton("Load","Make Test1=Two and fill in DynamicTwo with B")
),
tabPanel(
"panel2",
uiOutput("DynamicOnTest1")
)
)
server <- function(input, output, session) {
output$DynamicOnTest1<- renderUI({
lapply(input$Test1,function(x) {
selectizeInput(paste("Dynamic",x,sep=""),paste("Dynamic",x), choices=c("A","B","C"),multiple=TRUE)
})
})
observeEvent(input$Load ,{
updateSelectizeInput(session,"Test1",selected="Two")
#somehow I need to get the renderUI to run in here, before...
updateSelectizeInput(session,"DynamicTwo",selected="B")
})
}
shinyApp(ui = ui, server = server)
我在这里遇到了精神上的砖墙,所以任何帮助都会受到高度赞赏。
答案 0 :(得分:2)
它有两种解决方法,但根据您对应用的真正计划,它们可能不合适。但也许这可以帮助你通过你的智力砖墙。
observe
为动态输入创建一个观察者。
observeEvent(input$Load ,{
updateSelectizeInput(session,"Test1",selected="Two")
})
observe({
input[["DynamicTwo"]]
updateSelectizeInput(session,"DynamicTwo",selected="B")
})
shinyjs::delay
等待DynamicOnTest1
首先呈现,因此我们确定input[["DynamicTwo"]]
存在。
ui <- navbarPage(
title = "navbar",
id = "navbar",
tabPanel(
"panel1",
selectizeInput("Test1",label="Test1: UI on panel2 depends on this",choices=c("One","Two","Three"),multiple=T,selected="One"),
actionButton("Load","Make Test1=Two and fill in DynamicTwo with B")
),
tabPanel(
"panel2",
uiOutput("DynamicOnTest1")
),
shinyjs::useShinyjs()
)
server <- function(input, output, session) {
output$DynamicOnTest1<- renderUI({
lapply(input$Test1,function(x) {
selectizeInput(paste("Dynamic",x,sep=""),paste("Dynamic",x), choices=c("A","B","C"),multiple=TRUE)
})
})
observeEvent(input$Load ,{
updateSelectizeInput(session,"Test1",selected="Two")
shinyjs::delay(1000, updateSelectizeInput(session,"DynamicTwo",selected="B"))
})
}
shinyApp(ui = ui, server = server)