Use input from UI element from string of ID using input$<id>

I have a generic function to create a series of inputs with from a vector of IDs / names. I want to use that vector of IDs to create a table showing the each of these inputs. My issue is that I can't figure out how to use input$<id> to return the actual input. I tried pasting strings (e.g., paste0("input$id", input_names[1]) and converting to other types but no luck.

The goal is to change the input_names variable and have those changes propagate to the dependent app elements.

library(shiny)
library(tidyverse)

generic_numeric_input <- function(id, ...){
    numericInput(inputId = id, label = id, ...)
}

input_names <- c("x", "y")

ex_inputs <- map(input_names, generic_numeric_input, value = 3)
    
shinyApp(
    ui = fluidPage(
        ex_inputs,
        tableOutput("ex_table")
    ),
    server = function(input, output, session) {
        output$ex_table <- renderTable(tibble(
            # how can I make this change along with input_names?
            "x" = input$x,
            "y" = input$y
        ))
    }
)

Created on 2021-04-25 by the reprex package (v2.0.0)

is this what you need?

  server = function(input, output, session) {
    output$ex_table <- renderTable(tibble(
      # how can I make this change along with input_names?
      "x" = input[[input_names[1]]],
      "y" = input[[input_names[2]]]
    ))
  }
1 Like

I poorly explained what I was after, but your answer solved the one I described. Thank you!

Here's a slight modification to be a bit more general:

library(shiny)
library(tidyverse)

generic_numeric_input <- function(id, ...){
  numericInput(inputId = id, label = id, ...)
}

input_names <- c("x", "y", "z")

ex_inputs <- map(input_names, generic_numeric_input, value = 0)

shinyApp(
  ui = fluidPage(
    ex_inputs,
    tableOutput("ex_table")
  ),
  server = function(input, output, session) {
    output$ex_table <- renderTable(
      tibble(
      inputId = input_names,
      value = map_dbl(input_names, ~input[[.x]]))
    )
  }
)

Created on 2021-04-25 by the reprex package (v2.0.0)

This topic was automatically closed 7 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.