 # Creating a vector of inputs with length equal to the number of inputs defined by the user

I have a dynamic numericInput - i.e. the number of numericInput's displayed in the UI depends on the some other user-defined variable "num", i.e.:

In UI:

``````uiOutput("out")
``````

In server:

``````  output\$out  <- renderUI({
num <- input\$num
lapply(1:num, function(i) {
numericInput(inputId = paste0("input_",i),
label   = h5(paste0("Input number",i)),
value   = 0)
})
``````

Now also in server, I then need shiny to create a vector which contains all of the inputs.

E.g., if num=2, I need shiny to create a vector such that:

``````my_vec_1 <- c(input\$input_1, input\$input_2)
``````

This vector will then be part of a list, which contains other vectors which will be generated through the same process:

``````my_list <- list(my_vec_1, my_vec_2, ... etc...)
``````

The reason that these vectors need to be part of a list is that they will be used as parameters for prior distributions using rstan.

I have tried to do it using paste0 and as.name, like this:

``````my_vec_1 <- c()

for (i in 1:num) {
my_vec_1[i] <- paste0("input\$input_",i)
}

as_name_vec <- Vectorize(as.name, SIMPLIFY = TRUE)
my_vec_1 <-  as_name_vec(my_vec_1)
my_list <- list(my_vec_1, my_vec_2, ... etc...)
``````

However, I get an error: "Error in data_list2array(x) : all elements of the list should be numeric".

Note: If the number of inputs was fixed (e.g., let's say 2 inputs for vector #1 and 1 for vector #2), this would be very easy - I could just do something like this:

``````my_vec_1 <- c(input\$input_1, input\$input_2)
my_vec_2 <- c(input\$input_1)

my_list <- list(my_vec_1, my_vec_2)
``````

I was wondering if anybody has any suggestions?

Thanks.

``````for (i in 1:num) {
my_vec_1[i] <- input[[paste0("input_",i) ]]
}
``````

and no need for any as.name business.

1 Like

Thank you! that worked

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.