简体   繁体   中英

R/Shiny : RenderUI in a loop to generate multiple objects

After the success of the dynamic box in shiny here : R/Shiny : Color of boxes depend on select I need you to use these boxes but in a loop. Example : I have an input file which give this :

  • BoxA
  • BoxB
  • BoxC

I want in the renderUI loop these values as a variable to generate dynamically a Box A, B and C. (if I have 4 value, i will have 4 boxes etC.)

Here is my actually code:

for (i in 1:nrow(QRSList))

{ get(QRSOutputS[i]) <- renderUI({ column(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]))
  })

The aim is to replace these box names to a variable like input$s010102 for example. But get and assign function does not exist.

Any idea ?

Thanks a lot

Here is an example how to generate boxes dynamically

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM