【发布时间】:2021-12-30 05:27:06
【问题描述】:
我正在使用shinydashboard 构建一个shiny 应用程序。其目的是在每个 tabItem()(即仪表板的每一页)内显示一组“卡片”(技术上为 box() 元素)。该应用程序由一个外部 .csv 文件(在此表示对象 dat)驱动,该文件 (1) 定义应用程序内的页面,(2) 指定每个页面内 box() 元素的数量。
我已经能够使用dat 中的类别成功创建一组tabItem(即页面)。从这里开始,我无法弄清楚如何为每个tabItem 动态添加正确数量的框。如果您检查dat,您将看到有两个类别(页面):蓝色和绿色。蓝色类别需要我渲染两个框(框 A 和框 B),而绿色类别需要我渲染框 C - E。因此,名为“蓝色”的页面应该渲染两个框,而名为“绿色”的页面应该渲染三个盒子。
有人可以帮助我使用以下代码,以便为正确的页面呈现正确的框吗?如果 box_name 和 box_desc 可以分别显示为 box() 标题和内容,则特别感谢!
library(shiny)
library(shinydashboard)
dat<-tibble::tibble(category = c("blue", "blue", "green", "green", "green"),
box_name = c("Box A", "Box B", "Box C", "Box D", "Box E"),
box_desc = c("Foo", "Bar", "Bar", "Foo", "Foo"))
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Blue", tabName = "blue", icon = icon("dashboard")),
menuItem("Green", icon = icon("th"), tabName = "green")
)
)
body <- dashboardBody(
uiOutput("render_reports")
)
header<-dashboardHeader()
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$render_reports <- renderUI({
pages <- lapply(unique(dat$category), function(name){
tabItem(tabName = name, fluidRow(box(
title = name, paste0("Something here about ", name), width = 12, solidHeader = TRUE, status = "primary"
)),
fluidRow(
box(title = "box_title here", "box_desc here")
))
})
items <- c(pages)
do.call(tabItems, items)
})
}
shinyApp(ui, server)
【问题讨论】:
标签: r shiny shinydashboard