tabItem cannot show the content when more functions in menuItem using shiny and shinydashboard

跟風遠走 提交于 2019-12-11 10:13:31

问题


I am learning shiny and shinydashboard. My code is like this:

library(shiny)
library(shinydashboard)
library(DT)
library(RODBC)
library(stringr)
library(dplyr)
ch<-odbcConnect('B1P HANA',uid='fchen4',pwd='XUEqin0312')
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Query1",tabName="Query1",icon=icon("table"),
         numericInput('Start1','Start Date',19800312,min=20170101,max=20200101),
         numericInput('End1','End Date',19800312,min=20170101,max=20200101),
         textInput('Office1','Office ID','0'),
         submitButton("Submit")),
    menuItem("Query2",tabName="Query2",icon=icon("table"),
         numericInput('Start2','Start Date',20180101,min=20170101,max=20200101),
         numericInput('End2','End Date',20180101,min=20170101,max=20200101),
         textInput('Office2','Office ID','0'),
         submitButton("Submit"))
  )
)
body <- dashboardBody(
  tabItems(
    tabItem(tabName="Query1",h2("Dashboard tab content")),
    tabItem(tabName = "Query2",h2("Widgets tab content"))
  )
)
ui <- dashboardPage(
  dashboardHeader(title = 'LOSS PREVENTION'),
  sidebar,
  body
)
server <- function(input, output) {
}
shinyApp(ui, server)

The dash board looks like this:

You can see that when I put some input boxes in side bar, The text cannot show in the main part.

However, when my code is like this:

library(shiny)
library(shinydashboard)
library(DT)
library(RODBC)
library(stringr)
library(dplyr)
ch<-odbcConnect('B1P HANA',uid='fchen4',pwd='XUEqin0312')
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Query1",tabName="Query1",icon=icon("table")),
    menuItem("Query2",tabName="Query2",icon=icon("table"))
  )
)
body <- dashboardBody(
  tabItems(
    tabItem(tabName="Query1",h2("Dashboard tab content")),
    tabItem(tabName = "Query2",h2("Widgets tab content"))
  )
)
ui <- dashboardPage(
  dashboardHeader(title = 'LOSS PREVENTION'),
  sidebar,
  body
)
server <- function(input, output) {
}
shinyApp(ui, server)

Then the result is like this:

You can see that now there is no inputs in the side bar, Then the main bar has text now. This is so weird.


回答1:


This is actually a pretty annoying thing in shinydashboard currently. There are some workaround solutions provided here by Winston Chang, but the best solution in my opinion is this one:

Basically what happens is, when you insert other input elements into a menuItem, it loses the data-toggle and data-value attributes. Because of this, tabItems in dashboardBody can't link with the menuItems anymore and thus the app can't display the content in the body.

You can use a custom function (convertMenuItem) to set data-toggle and data-value manually so that menuItems and tabItems are linked again.

Code:

library(shiny)
library(shinydashboard)

convertMenuItem <- function(mi,tabName) {
  mi$children[[1]]$attribs['data-toggle']="tab"
  mi$children[[1]]$attribs['data-value'] = tabName
  mi
}

sidebar <- dashboardSidebar(
  sidebarMenu(
    convertMenuItem(menuItem("Query1",tabName="Query1",icon=icon("table"),
             numericInput('Start1','Start Date',19800312,min=20170101,max=20200101),
             numericInput('End1','End Date',19800312,min=20170101,max=20200101),
             textInput('Office1','Office ID','0'),
             submitButton("Submit")), tabName = "Query1"),
    convertMenuItem(menuItem("Query2",tabName="Query2",icon=icon("table"),
             numericInput('Start2','Start Date',20180101,min=20170101,max=20200101),
             numericInput('End2','End Date',20180101,min=20170101,max=20200101),
             textInput('Office2','Office ID','0'),
             submitButton("Submit")), tabName = "Query2")
  )
)
body <- dashboardBody(
  tabItems(
    tabItem(tabName="Query1", h2("Dashboard tab content")),
    tabItem(tabName = "Query2", h2("Widgets tab content"))
  )
)
ui <- dashboardPage(
  dashboardHeader(title = 'LOSS PREVENTION'),
  sidebar,
  body
)

server <- function(input, output) {}
shinyApp(ui, server)

Result



来源:https://stackoverflow.com/questions/50245925/tabitem-cannot-show-the-content-when-more-functions-in-menuitem-using-shiny-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!