【问题标题】:ShinydashBoard rendered tabItem not showing properlyShinydashBoard 呈现的 tabItem 未正确显示
【发布时间】:2021-01-13 01:21:16
【问题描述】:

现在我在 shinydashboard 中遇到了 tabItem 的问题。我在侧边栏菜单中有一个菜单项,我想做的是当我点击动作按钮时,会添加一个新的菜单项和一个新的tabitem。但是,当我部署代码时,奇怪的是新渲染的tabitem内容只是将自己附加到第一个 tabitem。 我已经将 tabitem 放在 tabitems 中,但我仍然遇到这个问题。下面是代码:

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)


感谢谁能帮我解决这个问题。

【问题讨论】:

    标签: r shiny shinydashboard tabitem


    【解决方案1】:

    我已经更新了您的示例,使其可以正常工作。我的解决方案是设置您的dashboardBody,就好像您的动态呈现的选项卡已经存在,并带有tabItem 调用。然后只需在新标签的内容上使用renderUI。 “主”菜单项不存在似乎并不重要,您仍然可以在正文中设置选项卡来处理将在服务器端呈现的菜单项。

    我猜tabItems 只期望调用tabItem,所以在传递uiOutput 时你会得到意想不到的行为。

    下面的代码应该会给你你期望的行为:

    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)
    
    

    【讨论】:

    猜你喜欢
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多