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

shinydashboard中box()内容未占满全宽的问题如何解决?

问题原因

你遇到的是两个层面的宽度限制问题:

  1. verbatimTextOutput组件默认未配置为占满父容器的全部宽度
  2. R默认的打印输出有宽度阈值,超过阈值的内容会自动换行,即使外层容器宽度足够也无法铺满

修复方案

只需要对代码做两处调整即可:

  1. 给verbatimTextOutput添加width = "100%"参数,让输出容器占满外层box的宽度
  2. 在renderPrint中临时调整R的打印宽度阈值,适配容器宽度

修改后完整代码

library(shinydashboard)
library(MASS)

ui <- dashboardPage(
  dashboardHeader(title = "Clicking"),
  dashboardSidebar(collapsed = TRUE),
  dashboardBody(
    fluidRow(
      plotOutput("CerealPlot",
                 brush = "brushing"),
      box(verbatimTextOutput("coords", width = "100%"), width = 12)
    )
  )
)

server <- function(input, output) {
  output$CerealPlot <- renderPlot({
    plot(x = UScereal$protein, y = UScereal$calories,
         xlab = "Protein (gm)",
         ylab = "Calories",
         pch = as.character(UScereal$mfr))
  })
  
  output$coords <- renderPrint({
    # 调整打印宽度,数值可根据实际展示需求调整
    options(width = 150)
    brushedPoints(UScereal, input$brushing, xvar = "protein",
               yvar = "calories")
  })
}

shinyApp(ui, server)

如果后续需要展示更宽的表格内容,直接调大options(width = xxx)里的数值即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 23:45:08