1

我想创建一个使用navbarMenu()tabPanel()显示数据表的闪亮应用程序。app.R我打算使用闪亮模块的概念来创建R/tabUI.RR/tabServer.R生成这些表,而不是将所有代码都写在一个文件中。但是,我遇到了一个错误,无法弄清楚。任何建议和帮助表示赞赏!

我的代码:

### R/tabUI.R
tabUI <- function(id) {
    tagList(
        navbarMenu("display table",
            tabPanel(NS(id, "mtcars table"),
            DT::dataTableOutput("table")
        )
    )
 )
}


### R/tabServer.R
tabServer <- function(id) {
    moduleServer(id, function(input, output, session){
        output$table <- DT::renderDataTable(mtcars)
    })
}



### app.R
library(shiny)

ui <- navbarPage("dashboard",
    tabUI("table1")
)

server <- function(input, output, session){
    tabServer("table1")
}

shinyApp(ui=ui, server=server)



错误:

> runApp()
Error: Navigation containers expect a collection of `bslib::nav()`/`shiny::tabPanel()`s and/or `bslib::nav_menu()`/`shiny::navbarMenu()`s. Consider using `header` or `footer` if you wish to place content above (or below) every panel's contents.
4

1 回答 1

1

您不能tagList()在内部使用,navbarPage()因此您需要将其从模块中移除。

作为旁注,您应该ns <- NS(id)在模块的开头定义,然后将所有 id 包装在ns(). 在您的代码中,表 ID 未包含在内,ns()因此未显示。

固定代码:

### R/tabUI.R
tabUI <- function(id) {
  ns <- NS(id)
  
    navbarMenu("display table",
               tabPanel(ns("mtcars table"),
                        DT::dataTableOutput(ns("table"))
               )
    )
  
}


### R/tabServer.R
tabServer <- function(id) {
  moduleServer(id, function(input, output, session){
    output$table <- DT::renderDataTable(mtcars)
  })
}



### app.R
library(shiny)

ui <- navbarPage("dashboard",
                 tabUI("table1")
)

server <- function(input, output, session){
  tabServer("table1")
}

shinyApp(ui=ui, server=server)
于 2022-02-21T10:43:39.280 回答