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

如何在R Shiny的Box()组件中添加多行文本内容?

How to Add Multiple Lines of Text in a Shiny Box Component

No problem! There are a couple of straightforward ways to get multiple lines inside your Box. Here are the most common methods tailored to your needs:

Method 1: Use HTML Line Breaks (<br>)

Wrap your text in HTML() and use <br> tags to separate lines. This is the simplest approach for static text:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(
        HTML("line 1<br>line 2<br>line 3"),  # Modified to include line breaks
        status = "primary", 
        title = "Oral", 
        width = 6, 
        height = 120, 
        background = "red"
      )
    )
  )
)

server <- function(input, output) {}

shinyApp(ui, server)

Method 2: Use Paragraph Tags for Spaced Lines

If you want more visual spacing between lines, use tags$div with individual tags$p elements for each line. This adds proper paragraph spacing:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(
        tags$div(
          tags$p("line 1"),
          tags$p("line 2"),
          tags$p("line 3")
        ),
        status = "primary", 
        title = "Oral", 
        width = 6, 
        height = 120, 
        background = "red"
      )
    )
  )
)

server <- function(input, output) {}

shinyApp(ui, server)

Method 3: Dynamic Lines from a Vector

If your lines come from a dynamic list (like a variable or user input), use paste() with collapse = "<br>" and wrap in HTML():

library(shiny)
library(shinydashboard)

# Example dynamic line list
my_lines <- c("line 1", "line 2", "line 3")

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(
        HTML(paste(my_lines, collapse = "<br>")),
        status = "primary", 
        title = "Oral", 
        width = 6, 
        height = 120, 
        background = "red"
      )
    )
  )
)

server <- function(input, output) {}

shinyApp(ui, server)

All these methods will display your text on separate lines inside the Box. Pick the one that fits your use case best!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:16:22