How do I programmatically generate a data.frame from many Shiny inputs?

I'm trying to create a Shiny application that has ~100 user inputs, some of which may go untouched. I think that I could make a reactive dataframe within the app by writing input$user_input_1, input$user_input_2, etc. 100 times to call each input individually. Instead, I'm wondering if there is a way to programmatically generate that list of inputs using a concatenating function and then evaluate those inputs as if I had typed input$user_input_1. When I run the following code, however, I get a message telling me that Error: object 'input' not found.

My end goal is to be able to take these user inputs and use them in some behind-the-scenes transformations. If my current setup isn't the most efficient way to capture a large number of inputs, please let me know, but at the very least I'd like to understand what isn't working here.

I'd appreciate some help here. Thanks.

library(shiny)
library(tidyverse)

input_tbl <- tibble(inputs = c("x", "y", "z"))

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            numericInput(
                inputId = "number_x",
                label   = "Input X",
                value   = 30
            ),
            numericInput(
                inputId = "number_y",
                label   = "Input Y",
                value   = 60
            ),
            numericInput(
                inputId = "number_z",
                label   = "Input Z",
                value   = 90
            )
        ),

        mainPanel(
           verbatimTextOutput("table")
        )
    )
)

server <- function(input, output) {

    value_tbl <- eventReactive(
        eventExpr = input$number_x,
        valueExpr = {

            tibble(
              inputs = input_tbl %>% pull(),
              values = input_tbl %>%
                  pull() %>%
                  modify(~ str_c("input$number_", .x)) %>%
                  map(~ parse(text = .x)) %>%
                  map(eval)
            )

        })

    output$table <- renderPrint(value_tbl())
}

shinyApp(ui = ui, server = server)
1 Like

You can also access an input value via the input[["myvar"]] construction, I think that's easier to use in cases like this.

library(shiny)
library(tidyverse)

input_tbl <- tibble(inputs = c("x", "y", "z"))

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            numericInput(
                inputId = "number_x",
                label   = "Input X",
                value   = 30
            ),
            numericInput(
                inputId = "number_y",
                label   = "Input Y",
                value   = 60
            ),
            numericInput(
                inputId = "number_z",
                label   = "Input Z",
                value   = 90
            )
        ),

        mainPanel(
           verbatimTextOutput("table")
        )
    )
)

server <- function(input, output) {

    value_tbl <- eventReactive(
        eventExpr = input$number_x,
        valueExpr = {

            tibble(
              inputs = input_tbl %>% pull(),
              values = input_tbl %>%
                  pull() %>%
                  map(~ parse(text = input[[str_c("number_", .x)]])) %>%
                  map(eval) %>%
                  unlist()
            )

        })

    output$table <- renderPrint(value_tbl())
}

shinyApp(ui = ui, server = server)