直接动态添加框到 Shiny Dashboard

Dynamically adding boxes to Shiny Dashboard directly

我正在尝试根据向量的内容将多个框添加到闪亮的界面。

让我们从这里开始:

library(shiny)

ui <- fluidPage(

   titlePanel("Dynamic Boxes"),

   fluidRow(
     uiOutput("boxes")
  )
)

server <- function(input, output) {

  output$boxes <- renderUI({
    interf <- ""
    for(i in 1:10){
      x = 1:100
      interf <- box(title = paste0("box ", i), 
          renderPlot(plot(x = x, y = x^i)))

    }
    interf
  })
}

shinyApp(ui = ui, server = server)

它只显示最后一个框。我不知道如何将它们组合在一起,然后将其传递给客户端。

box 来自您尚未加载的 shinydashboard 包(至少在您的 post 中)。无论如何,您需要一个您的 for 循环不会创建的框元素列表。这是一种方式 -

library(shiny)
library(shinydashboard)

ui <- fluidPage(      
  titlePanel("Dynamic Boxes"),      
  fluidRow(
    uiOutput("boxes")
  )
)

server <- function(input, output) {      
  output$boxes <- renderUI({
    lapply(1:10, function(a) {
      x = 1:100
      box(title = paste0("box ", a), renderPlot(plot(x = x, y = x^a)))
    })
  })
}

shinyApp(ui = ui, server = server)