在闪亮的仪表板中使用 for 循环在框内打印文本

Print text using for loop inside box in shiny dashboard

我在下面有闪亮的仪表板,我想使用 for() 循环在 "Red1""Red21" 框内打印,就像屏幕截图中那样。 box() 应该用 renderUI()

创建

library(shiny)
library(shinydashboard)
shinyApp(
  ui = dashboardPage(
    options = list(sidebarExpandOnHover = TRUE),
    header = dashboardHeader(),
    sidebar = dashboardSidebar(minified = TRUE, collapsed = TRUE),
    body = dashboardBody(
      uiOutput("box1")
    ),
    title = "DashboardPage"
  ),
  server = function(input, output) {
    output$box1<-renderUI({

        box(
          for(i in 1:21){
            "Red"[i]
            br()
          },
          height = 300,width = 5
        )
    })
  }
)

box() 可以将列表作为第一个参数,因此您的代码可以这样重写:

...
box({
  text <- list()
  for(i in 1:21){
    text <- append(text, list(paste("Red", i), br()))
  }
  text
})
...

使用匿名函数(这是)执行此操作不利于可读性(至少对我而言),因此我建议您事先构建该列表。