shinydashboard中box()内容未占满全宽的问题如何解决?
问题原因
你遇到的是两个层面的宽度限制问题:
verbatimTextOutput组件默认未配置为占满父容器的全部宽度- R默认的打印输出有宽度阈值,超过阈值的内容会自动换行,即使外层容器宽度足够也无法铺满
修复方案
只需要对代码做两处调整即可:
- 给
verbatimTextOutput添加width = "100%"参数,让输出容器占满外层box的宽度 - 在
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
相关产品推荐
相关产品推荐

