Access an input variable through a string of its name (within a vector)

I am building a shiny application which has a dynamic user interface, meaning when a user specifies that there are 7 "concentrations" I have made it so that the tabPanel will generate 7 numericInput fields for the user to enter each of the concentrations. This feature works really well, however, I am having an issue accessing the data from each of those fields. I will provide more detail below. But in summary, I have a vector of strings, each of which is the name of one of my input variables. Is there a way for me to iterate through this vector of strings, and turn each one into the value of its respective variable?

Example:
I have set my numericInput() to 7, and so the user interface will generate 7 new numericInput fields using a schemed labeling system that iterates. I have written a method which takes the number from the original and generates a vector of labels. I would like to be able to access the value within each of these variables.

Here is the code that generates my vector of strings:

generate_labels <- function(trunk,num) { 
  list <- vector("character", num)
  for (i in seq_len(num)) { 
    list[i] <- (paste0("input$",trunk,"_", i))
  }
  list
}

So, a sample input could be:
c("input$var1", "input$var2", input$var3", input$var4", input$var5", input$var6", input$var7")

And they may correspond to
input$var1 = 1000
input$var2 = 500
input$var3 = 3
input$var4 = 400
input$var5 = 200
input$var6 = 1
input$var7 = 150

I would be able to ouput a vector that is:
c(1000, 500, 3, 400, 200, 1, 150)

Here is one of my stabs at it. I am assuming that I will require the function eval(), assign(), and or get().

a <- 1
b <- 2
c <- 3 

sting_list <- c("a", "b", "c")

find_value <- function(in_frame) { 
  #we cant to return a new function 
  out_frame <- vector("numeric", length(in_frame))
  for (i in 1:length(in_frame)) { 
    assign(out_frame[i], get(in_frame[i]))
  }
  out_frame
}

find_value(sting_list)

Please let me know if you have any suggestions, or if there is any problem with the problem I have provided. i would be happy to make the appropriate edits.

Thanks in advanced,

Chris

Let's see if I follow you. You make 7 numericInputs with the inputIds var1, var2, ... var7. You want to get the values of those seven inputs into a vector. Try

string_list <- c("var1", "var2", "var3", "var4", "var5", "var6", "var7")
Vals <- vector("numeric", length = length(string_list))
for (i in seq_along(string_list)) {
  Vals[i] <- input[[string_list[i]]]
}
I have not actually tried that but I think it will work.
1 Like

Thanks very much for the assistance. I am so used to just writing input$variable that I had entirely missed what that expression indicated. I have pasted below the solution to the problem I had, which allows you to have a dynamic amount of strings. The problem I am now facing is that this function must be placed in the server function due to scoping rules. I wonder if there is a way to allow a function in the main body of the application access to the input objects.

  generate_values <- function(trunk,num) { 
    list <- vector("numeric", num)
    for (i in seq_len(num)) {
      list[i] <- input[[(paste0(trunk, i))]]
    }
    list
  }

Thanks again!

Chris

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