1

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

SMAX
  • 73
  • 3
  • 12

1 Answers1

6

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)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Thanks ! It works Well. Just, how to take the value of one of these boxes ? – SMAX Aug 08 '18 at 09:18
  • 1
    You can access the `selectInput` created by those via `input$slider1`, `input$slider2` and so on, as you can see those are created with a loop`paste0("slider",i)` – Pork Chop Aug 08 '18 at 09:28
  • 1
    Thats it :) QRSList$Statut[i] = input[[paste0('QRSSelect', i)]] – SMAX Aug 08 '18 at 11:10
  • In the same loop you can as well create an obs[[paste0("slider",i)]] <<-observeEvent(input[[paste0("slider",i)]], {# Catch input here...}) where obs = list() # in Shiny Server – Saren Tasciyan Apr 16 '20 at 17:36
  • @Genom that can create a memory leak, check your memory print once u create 1000 of them – Pork Chop Apr 16 '20 at 17:50
  • Yes, but I could not find another way, if I don't know the number of groups required. I usually don't create that many. But I also don't want to put a hard coded limit. The idea of keeping them in a list was also to remove them. – Saren Tasciyan Apr 16 '20 at 21:40