如何创建一个闪亮的应用程序,其中选项卡仅在满足条件时才可见
How to create a shiny App where tabs are only become visible if a condition is met
我想创建一个有两个选项卡的 ShinyApp,每个选项卡中都有一个问题需要回答。如果用户事先正确回答了第一个问题,则用户应该只看到带有第二个问题的第二个选项卡。
是否有可能仅当用户之前在“问题 1”选项卡中正确回答了第一个问题时才会显示“问题 2”选项卡?
我创建了一个最小示例,但我没能隐藏第二个选项卡。
library(shiny)
ui <- shinyUI(pageWithSidebar(
headerPanel("Header"),
sidebarPanel(
conditionalPanel(
condition = "input.tabselected ==1",
sliderInput(
inputId = "slider1",
label = "Slider 1",
min = 0,
max = 1,
value = 0.5
),
selectInput(
inputId = "quiz1",
label = " Right or wrong",
selected = NULL,
choices = c("Right",
"Wrong")
),
conditionalPanel(
"input.quiz1 === 'Right'",
paste0("Feedback: Right"),
actionButton("Tab2", label = "Next Question")
),
conditionalPanel(
"input.quiz1 === 'Wrong'",
paste0("Feedback: Wrong")
),
conditionalPanel(
condition = "input.tabselected ==2",
sliderInput(
inputId = "slider2",
label = "Slider 2",
min = 0,
max = 1,
value = 0.5
),
selectInput(
inputId = "quiz2",
label = "True or false",
selected = NULL,
choices = c("false",
"true")
),
conditionalPanel(
"input.quiz2 === 'true'",
paste0("Feedback: Right answer")
),
conditionalPanel(
"input.quiz2 === 'false'",
paste0("Feedback: Wrong answer")
) ) ),
mainPanel(
tabsetPanel(
type = "tabs",
tabPanel(
"Question 1",
titlePanel("Description question 1"),
value = "1"
),
tabPanel(title = "Question 2" ,
value = "2",
conditionalPanel("input.quiz1 === 'Right'",
titlePanel("Description question 2")
)
),
id = "tabselected"
))))
server = function(input, output, session) {
observeEvent(input$Tab2, {
updateTabsetPanel(session, "tabselected",
selected = "2")
})
}
shinyApp(ui = ui, server = server)
您可以在下面的 link 中找到条件选项卡集的解决方案,这是在服务器端使用 renderUI()
完成的,并显示了使用条件面板实现的
可以使用以下代码(源自原始 link 中的示例)实现向现有选项卡集添加选项卡的更详细示例:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
checkboxGroupInput("Tabs", label = h4("tabpanel"), choices = list("tabs" = "tabs"),selected = NULL),
checkboxGroupInput("MoreTabs", label = h4("moretabpanel"), choices = list("moretabs" = "moretabs"),selected = NULL)
),
dashboardBody(
tabBox(id="x",tabPanel("panel","panel"))
)
)
server <- function(input, output) {
observeEvent(input$Tabs,{
check1 <- input$Tabs == "tabs"
if(check1&!is.na(check1)){
insertTab(inputId = "x",
tabPanel("Dynamic", "This a dynamically-added tab"),target="panel",position ="after")
}
})
}
shinyApp(ui, server)
我们使用函数 insertTab
根据事件将选项卡附加到现有选项卡集。 (选中一个框)如果您希望在条件再次不满足时删除此选项卡,您可以使用 removeTab
有关这些功能的更多信息,请参见此处:
我想创建一个有两个选项卡的 ShinyApp,每个选项卡中都有一个问题需要回答。如果用户事先正确回答了第一个问题,则用户应该只看到带有第二个问题的第二个选项卡。
是否有可能仅当用户之前在“问题 1”选项卡中正确回答了第一个问题时才会显示“问题 2”选项卡?
我创建了一个最小示例,但我没能隐藏第二个选项卡。
library(shiny)
ui <- shinyUI(pageWithSidebar(
headerPanel("Header"),
sidebarPanel(
conditionalPanel(
condition = "input.tabselected ==1",
sliderInput(
inputId = "slider1",
label = "Slider 1",
min = 0,
max = 1,
value = 0.5
),
selectInput(
inputId = "quiz1",
label = " Right or wrong",
selected = NULL,
choices = c("Right",
"Wrong")
),
conditionalPanel(
"input.quiz1 === 'Right'",
paste0("Feedback: Right"),
actionButton("Tab2", label = "Next Question")
),
conditionalPanel(
"input.quiz1 === 'Wrong'",
paste0("Feedback: Wrong")
),
conditionalPanel(
condition = "input.tabselected ==2",
sliderInput(
inputId = "slider2",
label = "Slider 2",
min = 0,
max = 1,
value = 0.5
),
selectInput(
inputId = "quiz2",
label = "True or false",
selected = NULL,
choices = c("false",
"true")
),
conditionalPanel(
"input.quiz2 === 'true'",
paste0("Feedback: Right answer")
),
conditionalPanel(
"input.quiz2 === 'false'",
paste0("Feedback: Wrong answer")
) ) ),
mainPanel(
tabsetPanel(
type = "tabs",
tabPanel(
"Question 1",
titlePanel("Description question 1"),
value = "1"
),
tabPanel(title = "Question 2" ,
value = "2",
conditionalPanel("input.quiz1 === 'Right'",
titlePanel("Description question 2")
)
),
id = "tabselected"
))))
server = function(input, output, session) {
observeEvent(input$Tab2, {
updateTabsetPanel(session, "tabselected",
selected = "2")
})
}
shinyApp(ui = ui, server = server)
您可以在下面的 link 中找到条件选项卡集的解决方案,这是在服务器端使用 renderUI()
完成的,并显示了使用条件面板实现的
可以使用以下代码(源自原始 link 中的示例)实现向现有选项卡集添加选项卡的更详细示例:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
checkboxGroupInput("Tabs", label = h4("tabpanel"), choices = list("tabs" = "tabs"),selected = NULL),
checkboxGroupInput("MoreTabs", label = h4("moretabpanel"), choices = list("moretabs" = "moretabs"),selected = NULL)
),
dashboardBody(
tabBox(id="x",tabPanel("panel","panel"))
)
)
server <- function(input, output) {
observeEvent(input$Tabs,{
check1 <- input$Tabs == "tabs"
if(check1&!is.na(check1)){
insertTab(inputId = "x",
tabPanel("Dynamic", "This a dynamically-added tab"),target="panel",position ="after")
}
})
}
shinyApp(ui, server)
我们使用函数 insertTab
根据事件将选项卡附加到现有选项卡集。 (选中一个框)如果您希望在条件再次不满足时删除此选项卡,您可以使用 removeTab
有关这些功能的更多信息,请参见此处: