我有一个闪亮的应用程序,有两个 selectInput(L1 和 L2),其中一个观察者使用 updateSelectInput 根据 L1 的选择更新 L2。我还有一个依赖于这两个选择的 renderPlot 输出。我面临的问题是,每当我更改 L1 时,renderPlot 都会被调用两次,一次使用 L2 的旧值,一次使用新值(在 updateSelectInput 中设置)。我的代码如下:
ui.R
shinyUI(
fluidPage(
titlePanel("Nested Selects Problem"),
sidebarLayout(
sidebarPanel(
selectInput(
"L1",
label = "L1",
choices = c("red", "blue")
),
selectInput(
"L2",
label = "L2",
choices = ""
)
),
mainPanel(
plotOutput("plot")
)
)
)
)
服务器.R
shinyServer(
function(input,output,session) {
observe({
if (input$L1 == "red") {
choices <- c(1000000,2000000,3000000)
}
else {
choices <- c(10,20,30)
}
updateSelectInput(session,"L2",choices=choices,selected=choices[1])
})
output$plot <- renderPlot({
if (input$L2 != "") {
plot(runif(as.numeric(input$L2)),col=input$L1)
}
})
})
如何避免第一次调用 renderPlot?在我看来,如果我可以安排observe()在第一个renderPlot之前调用,我就会得到想要的效果。
谢谢您的帮助。
您可以在
isolate
内的 input$L1
通话中使用 renderPlot
。以这种方式,仅当 updateSelectInput
更改时,更改才应从您的 input$L1
调用传播:
library(shiny)
runApp(list(
ui = fluidPage(
titlePanel("Nested Selects Problem"),
sidebarLayout(
sidebarPanel(
selectInput("L1",label = "L1",choices = c("red", "blue")),
selectInput("L2",label = "L2",choices = "")
),
mainPanel(
plotOutput("plot")
)
)
)
, server = function(input,output,session) {
observe({
if (input$L1 == "red") {
choices <- c(100,200,300)
}
else {
choices <- c(10,20,30)
}
updateSelectInput(session,"L2",choices=choices,selected=choices[1])
})
output$plot <- renderPlot({
if (input$L2 != "") {
plot(runif(as.numeric(input$L2)),col=isolate(input$L1))
}
})
})
)
那么,怎么样:
shinyServer(
function(input,output,session) {
L1_selected <- reactiveValues(triggered=-1)
observe({
if (input$L1 == "red") {
choices <- c(10, 100,200,300)
}
else {
choices <- c(10,20,30)
}
old_L2 <- isolate(input$L2)
updateSelectInput(session,"L2",choices=choices,selected=choices[1])
isolate(L1_selected$triggered <- L1_selected$triggered + as.numeric(old_L2 != choices[1]))
})
output$plot <- renderPlot({
if (input$L2 != "") {
if (isolate(L1_selected$triggered)) {
isolate(L1_selected$triggered <- L1_selected$triggered - 1)
return()
} else {
plot(runif(as.numeric(input$L2)),col=input$L1)
}
}
})
})