我的代码无法按预期工作。让我描述一个演示该问题的场景。
用户在第一个选项卡上选择“操作1”。然后他/她在第二个选项卡上选择“选择1”。显示“选择节点”和“选择变量”。 --->这是预期的。
同一用户返回第一个标签并选择“操作2”。当他/她进入第二个标签时,他/她选择“选择3”。现在,仍然显示“选择节点”和“选择变量”。 --->这不是正确的。它们只应在用户选择“选择1”或“选择2”时出现。
所以,我的Shiny应用程序似乎记得以前的选择。知道如何更新我的代码以便按预期工作吗?谢谢!
server.R:
function(input, output, session) {
# Update SelectInput
observe({
if (input$action=="1") {
updateSelectInput(session, "selectnode", choices = "Total")
} else {
updateSelectInput(session, "selectnode", choices = c('Node 1', 'Node 2', 'Node 3'))
}
})
# Update SelectInput for selectvar
observe({
updateSelectInput(session, "selectvar", choices = c('A','B','C'))
})
observeEvent(input$act_next, {
updateTabsetPanel(session, "allResults", 'structure')
})
}
ui.R:
fluidPage(theme = shinytheme("spacelab"),
navbarPage("Structure", id = "allResults",
tabPanel(value='action', title='User Actions',
verticalLayout(
h4("Select an action"),
radioButtons("action", label = "",
choices = list("Action 1" = 1,
"Action 2" = 2),
selected = 1),
br(),
actionButton("act_next", "Next!")
)
),
tabPanel(value='structure', title='Main',
verticalLayout(
conditionalPanel(
condition = ("input.action==1"),
selectInput("dd_options", label = "Select one option",
choices = c(
"Choice 1" = 1,
"Choice 2" = 2),
selected = 1)
),
conditionalPanel(
condition = ("input.action==2"),
selectInput("dd_options", label = "Select one option",
choices = c(
"Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3),
selected = 3)
),
br(),
conditionalPanel(
condition = ("input.dd_options == 1 || input.dd_options == 2"),
selectInput("selectnode", label = "Select a node", choices = ""),
br()
),
conditionalPanel(
condition = ("input.dd_options == 1 || input.dd_options == 2" ),
selectInput("selectvar", label = "Select a variable", choices = ""),
br()
)
)
)))
答案 0 :(得分:0)
我认为您的问题是由于重复dd_options
。每this answer,输入标识符(和输出标识符)不能重复。
相反,我会使用根据您的input$action
更改的反应式用户界面。我还整合了您的conditionalPanel
。
试试这个:
library(shiny)
library(shinythemes)
ui <- fluidPage(theme = shinytheme("spacelab"),
navbarPage("Structure", id = "allResults",
tabPanel(value='action', title='User Actions',
verticalLayout(
h4("Select an action"),
radioButtons("action", label = "",
choices = list("Action 1" = 1,
"Action 2" = 2),
selected = 1),
br(),
actionButton("act_next", "Next!")
)
),
tabPanel(value='structure', title='Main',
verticalLayout(
uiOutput("dd_options"),
conditionalPanel(
condition = ("input.dd_options == 1 || input.dd_options == 2"),
selectInput("selectnode", label = "Select a node", choices = ""),
br(),
selectInput("selectvar", label = "Select a variable", choices = "")
)
))))
server <- function(input, output, session) {
# Update SelectInput
observe({
if (input$action==1) {
updateSelectInput(session, "selectnode", choices = "Total")
} else {
updateSelectInput(session, "selectnode", choices = c('Node 1', 'Node 2', 'Node 3'))
}
})
# Update SelectInput for selectvar
observe({
updateSelectInput(session, "selectvar", choices = c('A','B','C'))
})
observeEvent(input$act_next, {
updateTabsetPanel(session, "allResults", 'structure')
})
# reactive UI for dd_options choices
output$dd_options <- renderUI({
if(input$action == 1) {
choices <- c("Choice 1" = 1,
"Choice 2" = 2)
selected <- 1
} else if(input$action == 2) {
choices <- c("Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3)
selected <- 3
}
selectInput("dd_options", label = "Select one option",
choices = choices,
selected = selected)
})
}
shinyApp(ui, server)