1

While trying to create a series of InfoBoxes for a Shiny project, I've come across a loop that even though I've endlessly tried to solve, ends up in me pasting the code as many times as InfoBoxes I need, which is incredibly. My original chunk of code is something like this

output$**name** <- renderValueBox({
  InformacionIE2 <- InformacionIE2.df[ which(InformacionIE2.df$INSTITUCIONEDUCATIVA==input$input_typen),]
  if (InformacionIE2$**name** =="Si") {
    infoBox("Planos Electricos",
            color = "green",
            icon = icon("thumbs-up", lib = "glyphicon"),
            print (InformacionIE2$**name**)
    )
  } else {
    infoBox("**name** ",
            color = "red",
            icon = icon("thumbs-down", lib = "glyphicon"),
            print (InformacionIE2$**name**)
    )          
  }
})

What I need is to find a way for name1, name2, name3, ..., nameN to iterate in place of name in that exact same chunk of code without having to copy paste as namy times as names there are. I tried to do this with a simple loop over a list, but get back an error telling me there is a syntax error near unexpected token.

How could this be solved?

  • access outout via $ string will not work. Try output[["name"]] instead, same for InformacionIE2. If that does not help, a reproducible app would be helpful. – Tonio Liebrand Jan 18 '17 at 17:04

1 Answers1

2

Use lapply instead of a for loop and it should work. No clue why:

lapply(**names**, function(name) {
output[[name]] <- <- renderValueBox({
  InformacionIE2 <- InformacionIE2.df[ which(InformacionIE2.df$INSTITUCIONEDUCATIVA==input$input_typen),]
  if (InformacionIE2[[name]] =="Si") {
    infoBox("Planos Electricos",
            color = "green",
            icon = icon("thumbs-up", lib = "glyphicon"),
            print (InformacionIE2[[name]])
    )
  } else {
    infoBox(name,
            color = "red",
            icon = icon("thumbs-down", lib = "glyphicon"),
            print (InformacionIE2[[name]])
    )          
  }
})
}) 
Carl
  • 5,569
  • 6
  • 39
  • 74