R / Shiny:RenderUI循环生成多个对象

时间:2018-08-08 07:59:40

标签: html r user-interface shiny

在成功显示动态框之后,这里是R/Shiny : Color of boxes depend on select,我需要您循环使用这些框。 范例: 我有一个输入文件,给出了这个:

  • BoxA
  • BoxB
  • BoxC

我想在renderUI循环中将这些值作为变量来动态生成Box A,B和C。(如果我有4个值,那么我将有4个etC。)

这是我的实际代码:

for (i in 1:nrow(QRSList))

{     get(QRSOutputS [i])<-renderUI({       栏(4,       box(title = h3(QRSList [1],style =“ display:inline; font-weight:bold”),

      selectInput("s010102i", label = NULL,
                  choices = list("Non commencé" = "danger", "En cours" = "warning", "Terminé" = "success"),
                  selected = 1) ,width = 12, background = "blue", status = get(QRSIntputS[i])))
  })
  column(4,
observeEvent(input$s010102i,{
  get(QRSOutputS[i]) <- renderUI({
    box(title = h3(QRSList[1], style = "display:inline; font-weight:bold"),

        selectInput("s010102i", label = NULL,
                    choices = list("Not good" = "danger", "average" = "warning", "good" = "success"),
                    selected = get(QRSIntputS[i])) ,width = 12, background = "blue",status = get(QRSIntputS[i]))
  })

目标是将这些框名替换为例如input $ s010102之类的变量。但是获取和分配功能不存在。

有什么主意吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

这是一个如何动态生成框的示例

library(shinydashboard)
library(shiny)

QRSList <- c("Box1","Box2","Box3","Box4","Box5")

ui <- dashboardPage(
  dashboardHeader(title = "render Boxes"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Test", tabName = "Test")
    )
  ),

  dashboardBody(
    tabItems(
      tabItem(tabName = "Test",
              fluidRow(
                tabPanel("Boxes",uiOutput("myboxes"))
              )  
      )
    )   
  )
)


server <- function(input, output) {

  v <- list()
  for (i in 1:length(QRSList)){
    v[[i]] <- box(width = 3, background = "blue",
                  title = h3(QRSList[i], style = "display:inline; font-weight:bold"),
                  selectInput(paste0("slider",i), label = NULL,choices = list("Not good" = "danger", "average" = "warning", "good" = "success"))
    )
  }
  output$myboxes <- renderUI(v)
}

shinyApp(ui = ui, server = server)

enter image description here