Search code examples
rdataframeshinydashboard

How to attach `tabPanel` to `menuSubItem`?


I have two data frames put in a single tabpanel appearing on a dashboardBody. Can someone help me attach this tabpanel to the menuSubItem titled "Inventory" appearing on a dashboardSidebar as given in the following script?

Also I need a help to create a space between the two data frames since they are located in a single tabpanel one over the other.

df1 = data.table(
    "Сolumn1"= as.character(),
    "Column2"= as.numeric())

df2 = data.table(
    "Сolumn1"= as.character(),
    "Column2"= as.numeric())


ui <- dashboardPage(
  dashboardHeader(title = "Assets"),
  dashboardSidebar(
    menuItem("Home", tabName = "home"),
    menuItem("Current assets", tabName = "CurrentAssets",
    menuSubItem("Inventory", tabName="inventory")
   )
  ),
  dashboardBody(
    tabsetPanel(
      tabPanel(tabName = "inventory",
        "df1",
        rHandsontableOutput("Table1")
        ),
    tabsetPanel(
      tabPanel(tabName = "inventory",
        "df2",
        rHandsontableOutput("Table2")
    )
      )
    )
  )
)


server <- function(input, output, session) {

    output$Table1 <- renderRHandsontable({
            rhandsontable(df1)
  })

    output$Table2 <- renderRHandsontable({
            rhandsontable(df2)
  })

    }

shinyApp(ui, server)

Solution

  • To connect or attach a tab with a menuItem or subMenuItem you have to use tabItems and tabItem instead of tabSetPanel and tabPanel. For the layout you could wrap each rHandsontable in column to use a column layout and to set the width or the number of columns (there are 12 at max) to use for this table.

    library(shinydashboard)
    library(shiny)
    library(rhandsontable)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Assets"),
      dashboardSidebar(
        menuItem("Home", tabName = "home"),
        menuItem("Current assets",
          tabName = "CurrentAssets",
          menuSubItem("Inventory", tabName = "inventory")
        )
      ),
      dashboardBody(
        tabItems(
          tabItem(
            tabName = "inventory",
            column(
              width = 3,
              "df1",
              rHandsontableOutput("Table1")  
            ),
            column(
              width = 3,
              "df2",
              rHandsontableOutput("Table2")
            )
          )
        )
      )
    )
    
    server <- function(input, output, session) {
      output$Table1 <- renderRHandsontable({
        rhandsontable(df1)
      })
    
      output$Table2 <- renderRHandsontable({
        rhandsontable(df2)
      })
    }
    
    shinyApp(ui, server)
    

    enter image description here