You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R Shiny中基于数据框动态渲染InfoBox与分类标签页?

Great question! You can use Shiny's renderUI and uiOutput to dynamically generate infoBox components grouped into category-based tab panels—just like how you used renderMenu for dropdown tasks. Here's a complete, adapted version of your code that does exactly what you're asking for:

library(shiny)
library(shinydashboard)
library(dplyr)

apps_directory <- data.frame(
  category = c('Movies','Books','Movies','Movies','Books'),
  title = c('Lord of the Rings','Neverending story','Batman','Superman','The little prince'),
  description = c('This is an epic fantasy trilogy.', 'A timeless tale of imagination.', 'The caped crusader\'s adventures.', 'The man of steel saves the day.', 'A classic story about friendship.'),
  icon = c('film', 'book', 'film', 'film', 'book'),
  PageURL = c('http://www.google.com', 'http://www.google.com', 'http://www.google.com','http://www.google.com','http://www.google.com')
)

header <- dashboardHeader(disable = TRUE)
sidebar <- dashboardSidebar(disable = TRUE)
body <- dashboardBody(
  uiOutput("category_tabs")
)

server <- function(input, output) {
  output$category_tabs <- renderUI({
    # Split data into groups by category (no hardcoding!)
    category_groups <- split(apps_directory, apps_directory$category)
    
    # Create a tab panel for each category
    tab_list <- lapply(names(category_groups), function(category_name) {
      current_data <- category_groups[[category_name]]
      
      # Generate infoBoxes for every row in the category
      info_boxes <- lapply(1:nrow(current_data), function(row_idx) {
        row <- current_data[row_idx, ]
        
        # Make title clickable (opens link in new tab)
        clickable_title <- HTML(paste0(
          "<a href='", row$PageURL, "' target='_blank'>",
          row$title,
          "</a>"
        ))
        
        infoBox(
          title = clickable_title,
          subtitle = row$description,
          icon = icon(row$icon),
          color = ifelse(category_name == "Movies", "red", "blue"), # Dynamic color per category
          fill = TRUE # Fill box with color for better visibility
        )
      })
      
      # Arrange infoBoxes into rows of 3 to avoid overflow
      rows <- split(info_boxes, ceiling(seq_along(info_boxes)/3))
      row_elements <- lapply(rows, function(row_items) {
        fluidRow(row_items)
      })
      
      # Build the tab panel for this category
      tabPanel(title = category_name, row_elements)
    })
    
    # Wrap all tabs in a full-width tabBox
    tabBox(
      width = 12,
      tab_list
    )
  })
}

ui <- dashboardPage(header = header, sidebar = sidebar, body = body )
shinyApp(ui, server)

Key Details & Customization Tips:

  • Dynamic UI Foundation: We use uiOutput as a placeholder in the UI, paired with renderUI on the server to build the tab structure and infoBoxes dynamically.
  • Category Auto-Generation: The split() function groups your data by category automatically—no need to hardcode tab names or filters.
  • Interactive InfoBoxes: Each infoBox title is a clickable link (using HTML() to render raw HTML) that opens in a new tab.
  • Layout Control: InfoBoxes are organized into rows of 3 to keep the dashboard clean; adjust the number in ceiling(seq_along(info_boxes)/3) to fit your needs.
  • Visual Tweaks: You can modify colors, icons, or toggle fill = FALSE to change the infoBox style.

内容的提问来源于stack exchange,提问作者Manuel Chirouze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:52:45