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?
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]])
)
}
})
})