1

我想创建一个带有两个选项卡的 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)

4

1 回答 1

1

您可以在下面的链接中找到条件选项卡集的解决方案,这是在服务器端完成的,renderUI()并通过条件面板实现

使用条件面板在闪亮的仪表板中添加动态选项卡

可以使用下面的代码(源自原始链接中的示例)来实现将选项卡添加到现有选项卡集的更接近的示例:

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在此处找到有关这些功能的更多信息:

闪亮的插入标签

于 2021-06-28T15:54:56.357 回答