首页 > 解决方案 > R闪亮组多个textOutputs

问题描述

我想将一些 textOutput() 值整齐地组合在一个框中。我知道这可以使用 shinydashboard::box() 来完成,但这意味着更改我的应用程序布局中的所有内容,而我不想这样做。有没有办法在基础闪亮本身中做到这一点?

我想在我的 textOutputs 周围有一个简单的框,最好与侧边栏面板分开并在其下方。

可重现的例子:

library(ggplot2)
library(shiny)

ui <- fluidPage(
  sidebarPanel(
    selectInput("var", choices = c("mpg", "disp"), label = "Selected Input"),
    textOutput("foo1"),
    textOutput("foo2"),
    textOutput("foo3"),
    textOutput("foo4")
 ),
  mainPanel(
    plotOutput("plot1", hover = "plot_hover")
)
)

server <- function(input, output) {
 output$plot1 <- renderPlot({
 ggplot(mtcars, aes_string(x = 'wt', y=input$var)) + geom_point()
})
output$foo1 <- renderText("This is text output 1")
output$foo2 <- renderText("This is text output 2")
output$foo3 <- renderText("This is text output 3")
output$foo4 <- renderText("This is text output 4")
}

shinyApp(ui, server)

和应用程序:

在此处输入图像描述

我想要的是:

在此处输入图像描述

一个单独的框,将我的 textOutputs 分组在一个位置,在 sidbarPanel 区域的外部和下方

标签: rshiny

解决方案


<div>您可以通过添加标签并为其添加边框来绘制简单的“框” 。例如

ui <- fluidPage(
  sidebarPanel(
    selectInput("var", choices = c("mpg", "disp"), label = "Selected Input"),
    div(style="border:1px solid red",
      textOutput("foo1"),
      textOutput("foo2"),
      textOutput("foo3"),
      textOutput("foo4")
    )
  ),
  mainPanel(
    plotOutput("plot1", hover = "plot_hover")
  )
)

或者您可以添加一个类属性。您只需要使用 CSS 自己进行样式设置。


推荐阅读