Using shiny modules and shinydashboard: shiny.tag error

14,373

Problem with shinydashboard and tagList

As described here

You need simple use

   ui <- dashboardPage(
  dashboardHeader(
    title = "Shiny modules and shinydashboard"
  ),

  dashboardSidebar(
    sidebarMenu(
      menuItem("PointA",tabName = "PointA") 
    )
  ),

  dashboardBody(
    tags$div( fooUI("myid"), class = "tab-content" )

)
)

Update

You also need tabName in menuItem

menuItem("PointA_",tabName = "PointA") 

Explanation

As you can see

tabItems
function (...) 
{
    lapply(list(...), tagAssert, class = "tab-pane")
    div(class = "tab-content", ...)
}
<environment: namespace:shinydashboard>

use list to ... and cant work if you already have list as arg.

So other variant it create new tabItems function like

tabItems1=function (...) 
    {
        lapply(..., tagAssert, class = "tab-pane")
        div(class = "tab-content", ...)
    }
environment(tabItems1)=environment(tabItems)

And then you can use tabItems1 with tagList

Share:
14,373
Manuel R
Author by

Manuel R

I am a research assistant at the chair of econometrics at the University of Wuerzburg. Interested in R, the tidyverse, Shiny, and anything related to making a shiny app beautiful.

Updated on July 23, 2022

Comments

  • Manuel R
    Manuel R almost 2 years

    I'm trying to create and use shiny modules inside a shinydashboard app but I keep getting this error:

    Error in FUN(X[[i]], ...) : Expected an object with class 'shiny.tag'.
    

    Here is the app as condensed as possible:

    ui.r

    library(shiny)
    library(shinydashboard)
    
    source("modules.r")
    
    ui <- dashboardPage(
     dashboardHeader(
      title = "Shiny modules and shinydashboard"
     ),
    
     dashboardSidebar(
      sidebarMenu(
       menuItem("PointA") 
      )
     ),
    
     dashboardBody(
      tabItems(
       fooUI("myid") 
      )
     )
    )
    

    server.r

    server <- function(input, output) {
     callModule(foo, "myid") 
    }
    

    modules.r

    fooUI <- function(id) {
     ns <- NS(id)
    
     tagList(
      tabItem(
       tabName = "PointA",
       textOutput(ns("text"))
      )
     )
    }
    
    foo <- function(input, output, session){
    
     output$text <- renderText(
      rnorm(1)
     )
    }
    

    What am I doing wrong? I got other kinds of modules to work in a "normal" shiny app, however, whenever I try to use a module within shinydashboard it fails.

    I am using the newest version of R, shiny and shinydashboard. This is not the issue here.

  • Manuel R
    Manuel R almost 8 years
    Using tags$div( fooUI("myid"), class = "tab-content" ) does not work. I get: Error in FUN(X[[i]], ...) : Expected tag to have class 'tab-pane. When I change the class attribute to "tab-pane" I get the app to run but no content is shown..
  • Manuel R
    Manuel R almost 8 years
    But this is the right direction, so thanks anyway :-)
  • Manuel R
    Manuel R almost 8 years
    Your second solution (redfining tabItems seems to work)....its kind of ugly though...
  • Batanichek
    Batanichek almost 8 years
    Hmm.. strange add full UI for first variant, it also not work?
  • Batanichek
    Batanichek almost 8 years
    Without tagList you cant use more than one element in fooUI
  • Manuel R
    Manuel R almost 8 years
    Hmm yes i just realized that :-(
  • Lazarus Thurston
    Lazarus Thurston about 3 years
    Seems this error Expected an object with class 'shiny.tag' comes not just in your specific scenario but a general probelm in shinydashboard.