首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在tabItem()中动态创建box()

在tabItem()中动态创建box()
EN

Stack Overflow用户
提问于 2021-11-19 12:25:54
回答 1查看 29关注 0票数 1

我正在用shinydashboard构建一个shiny应用程序。它的目的是在每个tabItem() (即仪表板的每一页)中显示一组“卡片”(从技术上讲是box()元素)。应用程序由一个外部.csv文件(在这个reprex中是object dat)驱动,该文件(1)定义应用程序中的页面,(2)指定每个页面中的box()元素的数量。

我已经成功地使用dat中的类别创建了一组tabItem(即页面)。从这里,我想不出如何为每个tabItem动态添加正确数量的框。如果你检查dat,你会看到有两个类别(页面):蓝色和绿色。蓝色类别需要我呈现两个框(框A和框B),而绿色类别需要我呈现框C- E。因此,名为“蓝色”的页面应该呈现两个框,而名为“绿色”的页面应该呈现三个框。

有人能帮助我使用下面的代码,以便为正确的页面呈现正确的框吗?如果box_name和box_desc可以分别作为box()标题和内容出现,我将非常感谢!

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-19 13:17:11

是否需要renderUI

在没有renderUI的情况下,它工作得很好(而且速度更快

代码语言:javascript
复制
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", tabName = "green", icon = icon("th"))
))

body <- dashboardBody({
  items <- lapply(unique(dat$category), function(name) {
    tabItem(tabName = name, fluidRow(
      lapply(which(dat$category %in% name), function(i) {
        box(
          dat$box_desc[i],
          title = dat$box_name[i],
          paste0("Something here about ", name),
          width = 12,
          solidHeader = TRUE,
          status = "primary"
        )
      }))
    )})
  do.call(tabItems, items)
})

header <- dashboardHeader()

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {}

shinyApp(ui, server)

使用renderUI时,只有在切换选项卡之后才会呈现项目-因为在第一次呈现dashboardBody时,框并不存在:

代码语言:javascript
复制
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", tabName = "green", icon = icon("th"))
))

body <- dashboardBody(uiOutput("renderReports"))
header <- dashboardHeader()

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {
  output$renderReports <- renderUI({
    items <- lapply(unique(dat$category), function(name) {
      tabItem(tabName = name, fluidRow(
        lapply(which(dat$category %in% name), function(i) {
        box(
          dat$box_desc[i],
          title = dat$box_name[i],
          paste0("Something here about ", name),
          width = 12,
          solidHeader = TRUE,
          status = "primary"
        )
      }))
    )})
    do.call(tabItems, items)
  })
}

shinyApp(ui, server)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70034827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档