Using shiny modules and shinydashboard: shiny.tag error

后端 未结 3 1880
孤城傲影
孤城傲影 2021-01-06 19:32

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 cl         


        
相关标签:
3条回答
  • 2021-01-06 20:14

    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

    0 讨论(0)
  • 2021-01-06 20:19

    Following @Batanichek's answer which pointed me to the source of the problem (thanks for that) I simply removed the tagList() command in my fooUI definition. This conveniently solves the problem!

    0 讨论(0)
  • 2021-01-06 20:30

    Two things:

    1. Seems tabName is necessary in the menuItem function
    2. Move tabItem from the module to ui (tabItem can hold you module)

    UI

    ui <- dashboardPage(
      dashboardHeader(
        title = "Shiny modules and shinydashboard"
      ),
    
      dashboardSidebar(
        sidebarMenu(
          menuItem("PointA", tabName = "PointA") 
        )
      ),
    
      dashboardBody(
        tabItems(
          tabItem("PointA",
                  fooUI("myid")
          )
        )
      )
    )
    

    Module

    fooUI <- function(id) {
      ns <- NS(id)
    
      tagList(
        tabName = "PointA",
        textOutput(ns("text"))
      )
    }
    
    foo <- function(input, output, session){
    
      output$text <- renderText(
        rnorm(1)
      )
    }
    
    0 讨论(0)
提交回复
热议问题