Using shiny modules and shinydashboard: shiny.tag error

好久不见. 提交于 2019-12-13 12:17:38

问题


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.


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/37593552/using-shiny-modules-and-shinydashboard-shiny-tag-error

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