【问题标题】:How to make a tabitem to be rendered only after the user clicks it - shiny R如何使 tabitem 仅在用户单击后才呈现 - 闪亮的 R
【发布时间】:2021-03-17 12:26:48
【问题描述】:

我一直在寻找解决方案,但我没有找到它

我的问题是我的仪表板看起来像这样:

即使我没有选择它们,它也会选择所有选项卡(如预渲染)

我尝试用一​​个按钮创建一个 observeEvent,但它不知道如何在他们点击后显示 UI。

我的代码是

library(shiny)
library(shinydashboard)


gamestop <- tags$img(src = "GSLL.png",
                     height = '30', width = '170')




ui <- dashboardPage(skin = "yellow",
                    
                    
                    
     dashboardHeader(title = gamestop,
                     dropdownMenu(type = "tasks",
                                  messageItem(
                                      from = "My contact",
                                      message = "x",
                                      icon = icon("glyphicon glyphicon-envelope"),
                                      href = "mailto:x"),
                                  messageItem(
                                      from = "Leads",
                                      message = "y",
                                      icon = icon("glyphicon glyphicon-envelope"),
                                      href = "mailto:y"),
                                  messageItem(
                                      from = "",
                                      message = "z",
                                      icon = icon("glyphicon glyphicon-envelope"),
                                      href = "mailto:z"),
                                      icon = icon("envelope")
                                  )
                     
                     ),
     
     
     
    dashboardSidebar( 
            sidebarMenu(
                        menuItem("Main menu", tabName = "main_menu", icon = icon("home")),
                        menuItem("Peripherals", tabName = "peripherals", icon = icon("hdd")),
                        menuItem("Database repair", tabName = "widgets", icon = icon("th")),
                        menuItem("Polling", tabName = "polling", icon = icon("cloud")),
                        menuItem("more issues!!", tabName = "issues", icon = icon("ad"))
                        
                        )
                    ),
     
                    
    dashboardBody(tags$head(
                  tags$link(rel = "stylesheet", type = "text/css", href = "Custom.css")),
                    
                        
                        
fluidPage(
                
                
                        tabItem(tabName = "peripherals",
                            h1("Peripherals",  
                                   style = "color:#15942B"),
                            
                            
                            tabsetPanel(type = "tabs",
                                        tabPanel("Printers",br(),
                                                 tabsetPanel(type = "tabs",
                                                             tabPanel("M452DW",br(),
                                                                      tabsetPanel(type = "tabs",
                                                                                  tabPanel("Not pinging"),
                                                                                  
                                                                                  
                                                                                  
                                                                                  tabPanel("Error messages")
                                                                                  )
                                                                      ),
                                                             tabPanel("M402DW",br(),
                                                                      tabsetPanel(type = "tabs",
                                                                                  tabPanel("Not pinging")
                                                                                  
                                                                                  ),
                                                                      
                                                                      
                                                                                  
                                                                      )
                                                 )
                                                 ),
                                        
                                        
                                        tabPanel("Pinpad",br(),
                                                 tabsetPanel(type = "tabs",
                                                             tabPanel("Offline / busy"),
                                                             
                                                             
                                                             tabPanel("Not turning on")
                                                             )
                                                 ),
                                        
                                        
                                        
                                        
                                        tabPanel("Scanners",br(),
                                                 tabsetPanel(type = "tabs",
                                                             tabPanel("GBT4400"),
                                                             
                                                             
                                                             
                                                             tabPanel("DS2278")
                                                             )
                                                 ),
                                        
                                        tabPanel("Receipt printer / cashdrawer",br(),
                                                 
                                                 tabsetPanel(type = "tabs",
                                                             tabPanel("Receipt printer"),
                                                             
                                                             tabPanel("Cash drawer")
                                                             )
                                                 ),
                                        
                                        tabPanel("Label printer",br(),
                                                 tabsetPanel(type ="tabs",
                                                             tabPanel("ZD410"),
                                                             tabPanel("LP2824 & +")
                                                             ),
                                                 
                                                 )
                                        )
                            
                            
                                ),    #Final tab peripherals
                
                
                tabItem(tabName = "main_menu",
                        h1("Main menu",  
                           style = "color:#15942B"),
                        strong("Here we can add the news of the day or a welcome image"),br(),
                        br(),
                        br(),
                        strong("This is a work in progress, to be presented to our team leads so we can make
                                   it an aid page for all of us")
                       ),
                
                
                tabItem(tabName = "issues",
                        h1("More issue resolutions to come!!!!!",
                           style  = "color:#15942B" ),
                        strong("My plan is to add the hardest issue resolutions for our team, so they can access this web page and
                               with a glipse they can resolve the issue in hand")
                        ),
                
                tabItem(tabName = "polling",
                        )
                
                
                
                            ) 
                        )
                    )
)







server <- function(input, output) {
    
observeEvent(input$tabs, 
             if(input$sidebarmenu == "Printers"){
    
})
    
    
    
    
    
}


shinyApp(ui, server)

我想知道当用户点击标签本身而不是之前如何呈现标签

非常感谢!!!

【问题讨论】:

  • 也许你在这里找到答案:community.rstudio.com/t/…
  • 我在您的代码中添加了第二个选项卡面板和选项卡面板,但问题仍然存在 - 当我单击第一个选项卡时,所有选项卡都被呈现或“激活”

标签: r shiny dashboard


【解决方案1】:

如果要在用户点击标签时渲染标签,需要观察tabsetpanel,检查标签是否被点击。

library(shiny)
library(shinydashboard)

ui <- fluidPage(
  
  tabItem(tabName = "peripherals",
          h1("Peripherals",  
             style = "color:#15942B"),
          
          
          tabsetPanel(id = "firsttabset",
                      type = "tabs",
                      tabPanel("Printers"),
                      tabPanel("Pinpad1", 
                               h1("Dies ist tab \"Pinpad1\""),
                               textOutput("text")),
                      tabPanel("Scanners"),
                      tabPanel("Receipt printer / cashdrawer"),
                      tabPanel("Label printer")
                      
          )
  )
  
)

server <- function(input, output) {
  
  observeEvent(input$firsttabset, {
               
               if(input$firsttabset == "Pinpad1") {
                 cat("tab \"Pinpad1\" is now being rendered \n")
                 output$text <- renderText({"tadaa"})
               } 
              
             
    })
  
 
}

shinyApp(ui, server)

编辑:如果您想将此应用于嵌套的选项卡集面板,我通过观察tabsetpanel1tabsetpanel2 并检查选择了哪些选项卡的条件找到了一种方法。我想tabsetpanel2的第一个标签,在这个例子中是Scanners2,如果你想渲染标签Pinpad1,就必须渲染。

看看它是否适合你。这个逻辑可以扩展到 tabsetpanels 的进一步嵌套,但是会变得复杂。

library(shiny)
library(shinydashboard)

ui <- fluidPage(
  
  tabItem(tabName = "peripherals",
          h1("Peripherals",  
             style = "color:#15942B"),
          
          
          tabsetPanel(id = "firsttabset",
                      type = "tabs",
                      tabPanel("Printers"),
                      tabPanel("Pinpad1", 
                               tabsetPanel(id = "secondtabset",
                                           type = "tabs",
                                           tabPanel("Scanners2",
                                                    h1("Dies ist tab \"Scanners2\"")),
                                           tabPanel("Pinpad2",
                                                    h1("Dies ist tab \"Pinpad2\""),
                                                    textOutput("text2"))),
                               h1("Dies ist tab \"Pinpad1\""),
                               textOutput("text")),
                      tabPanel("Scanners"),
                      tabPanel("Receipt printer / cashdrawer"),
                      tabPanel("Label printer")
                      
          )
  )
  
)

server <- function(input, output) {
  
  observeEvent(c(input$firsttabset,
                 input$secondtabset), {
               
               if(input$firsttabset == "Pinpad1" & input$secondtabset == "Scanners2") {
                 cat("tab \"Pinpad1\" is now being rendered \n")
                 output$text <- renderText({"tadaa"})
               } 
              
              if (input$firsttabset == "Pinpad1" & input$secondtabset == "Pinpad2") {
                 cat("tab2 \"Pinpad2\" is now being rendered \n")
                 output$text2 <- renderText({"tadooo"})
                 
               }
    
    })
  
  
}

shinyApp(ui, server)

【讨论】:

  • 我在您的代码中添加了第二个选项卡面板和选项卡面板,但问题仍然存在 - 当我单击第一个选项卡时,所有选项卡都被呈现或“激活”
猜你喜欢
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 2021-12-28
  • 2013-10-01
  • 1970-01-01
  • 2016-08-05
  • 2020-06-14
  • 2018-04-20
相关资源
最近更新 更多