如何在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
相关产品推荐
相关产品推荐

