Looping through shiny inputs by name using get not working, why?

I have a ten input values names inputs$Art1,...,input$Art10 that I am using to update values in a data.frame. Rather than write versions of the below 10 times, once for each input, is there a way to make the input dynamic. I get an object not found error this way:

# sample arts data.frame
Status<-rep("Proposed",10)
Use.P<-rep(1,10)
Use.R<-rep(0,10)
arts<-data.frame(Status,Use.P,Use.R)
# loop attempt
arts.calc<-reactive({
for(i in 1:nrow(arts)){
      if(get(paste0("input$Art",as.character(i)))=="Passed"){
        arts$Status[i]="Passed"
        arts$Use.P[i]<-0
      }
      if(get(paste0("input$Art",as.character(i)))=="Rejected"){
        arts$Status[i]="Rejected"
        arts$Use.P[i]<-0
        arts$Use.R[i]<-1
      }
    }
return(arts)
})

Sorry if I missed an answer elsewhere in the messages.

You can use the square brackets notation to access the values, since the input object is a list like object (reactiveValues).

I was not able to run your code unfortunately, so here's a simple example with some other data

library(shiny)
ui <- fluidPage(
    titlePanel("Bins output"),
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins1", "Number of bins:", 1, 10, 5),
            sliderInput("bins2", "Number of bins:", -100, 0, -50)
        ),
        mainPanel(
           textOutput("binsOutput")
        )
    )
)
server <- function(input, output) {
    output$binsOutput <- renderText({
        result <- "Bins output:"
        for (i in seq(2)) {
            result <- c(result, input[[paste0("bins", as.character(i))]])
        }
        result
    })
}
shinyApp(ui = ui, server = server)
1 Like

I can confirm that the input[["id.goes.here"]] method works. And if the ids you are interested in are in a string vector called ids, you can get them all with code like:

for(id in ids) {
   rawInput <- input[[id]]
... (then you should sanitize the rawInput by stripping HTML etc.)
...
}
2 Likes

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.