简体   繁体   English

ShinydashBoard 呈现的 tabItem 未正确显示

[英]ShinydashBoard rendered tabItem not showing properly

Now I have a issue with tabItem in shinydashboard.现在我在 Shinydashboard 中遇到了 tabItem 问题。 I have one menuitem in the sidebarmenu, what I want to do is that when I click the actionbutton and a new menuitem and a new tabitem will be added。 However, when I deploy the code, the strange thing is that the newly rendered tabitem content just appends itself to the first tabitem.我在sidebarmenu中有一个menuitem,我想要做的是当我点击actionbutton时会添加一个新的menuitem和一个新的tabitem。 但是,当我部署代码时,奇怪的是新渲染的tabitem内容只是将自己附加到第一个 tabitem。 I have already put tabitem in tabitems but I still got this problem.我已经将 tabitem 放在 tabitems 中,但我仍然遇到这个问题。 Below is the code:下面是代码:

library(shiny)
library(shinydashboard)

## ============================================ Define ui ==================================================

header1 <- dashboardHeader(
  title = "My Dynamic Menu"
) #dashboardHeader

# DYNAMIC UI
sidebar1 <- dashboardSidebar(
  sidebarMenu(
    menuItem('aa',tabName = 'aa')
  ) ,
  sidebarMenuOutput('bb')
) #dashboardSidebar
#
body1 <- dashboardBody(
  tabItems(
    tabItem(tabName = 'aa','aaa', actionButton('submit','Submit')),
    uiOutput('cc')
  ) #tabItems
) #dashboardBody

ui <- dashboardPage(header1, sidebar1, body1)

server <- function(input, output, session) {
  observeEvent(input$submit, {
    output$bb<-renderMenu({
      sidebarMenu(
        menuItem("Main", tabName = "main")
      )
    })
    
    output$cc<-renderUI({
      tabItem(tabName = "main",
              h2("Login"),
              textInput(inputId = "username1", label = "User name:", value = ""),
              passwordInput(inputId = "password1", label = "Password:"),
              actionButton(inputId = "loginbutton1", label = "Login")
      )
    })
  })
} #server
## ============================================ Run application ============================================
shinyApp(ui, server)


Thanks if anyone can help me address this problem.谢谢如果有人能帮我解决这个问题。

I've updated your example so that it should work.我已经更新了您的示例,以便它可以工作。 My solution is to set up your dashboardBody as if your dynamically rendered tab already exists with a tabItem call.我的解决方案是设置您的dashboardBody ,就好像您的动态呈现的选项卡已经存在并使用tabItem调用一样。 Then just use renderUI on the contents of your new tab.然后只需在新选项卡的内容上使用renderUI It doesn't seem to matter that the 'main' menu item doesn't exist yet, you can still set up tabs in the body to deal with menu items that will be rendered on the server side. “主”菜单项尚不存在似乎并不重要,您仍然可以在正文中设置选项卡来处理将在服务器端呈现的菜单项。

I guess tabItems only expects calls to tabItem , so you'll get unexpected behaviour when passing it uiOutput .我猜tabItems只需要调用tabItem ,所以在传递它uiOutput时你会得到意外的行为。

The following code should give you the behaviour you expect:以下代码应该为您提供您期望的行为:

library(shiny)
library(shinydashboard)

## ============================================ Define ui ==================================================

header1 <- dashboardHeader(
    title = "My Dynamic Menu"
) #dashboardHeader

# DYNAMIC UI
sidebar1 <- dashboardSidebar(
    sidebarMenu(
        menuItem('aa',tabName = 'aa')
    ) ,
    sidebarMenuOutput('bb')
) #dashboardSidebar
#
body1 <- dashboardBody(
    tabItems(
        tabItem(tabName = 'aa','aaa', actionButton('submit','Submit')),
        tabItem(tabName = "main", uiOutput('cc')) # put a tabItem here
    ) #tabItems
) #dashboardBody

ui <- dashboardPage(header1, sidebar1, body1)

server <- function(input, output, session) {
    observeEvent(input$submit, {
        output$bb<-renderMenu({
            sidebarMenu(
                menuItem("Main", tabName = "main")
            )
        })
        
        output$cc<-renderUI({ #render just the content of your tab here
            tagList(
                h2("Login"),
                textInput(inputId = "username1", label = "User name:", value = ""),
                passwordInput(inputId = "password1", label = "Password:"),
                actionButton(inputId = "loginbutton1", label = "Login")   
            )
        })
    })
} #server
## ============================================ Run application ============================================
shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM