Creating variable names for reactiveValues on the fly and then accessing their values? R Shiny

I have a shiny app that creates numerous reactive values variables depending on other user input. I named these variables in a loop with the assign() function. Ex: list_index1, list_index2, list_index3,... I cannot figure out to access the value of these variable in a loop. Of course i could do list_index3$value, but I want to be able to access everyone of the variables' values in a loop. If I try something like:

paste("list_num", 2, sep="")$value

I get the error

Warning: Error in $: $ operator is invalid for atomic vectors

I'm not sure how to go about doing this. Any help is appreciated. The code for this section of the app is below. Thanks.

test_var <<- 3
observeEvent(input$numoc, {
test_var <<- input$numoc
})

for (i in 1:test_var) {
assign(paste("list_num", i, sep=""),reactiveValues(value=NULL))
assign(paste("list_index", i, sep=""), reactiveValues(value=NULL))
list_num <- reactiveValues(value=NULL)
list_index <- reactiveValues(value=NULL)
}

for (i in 1:3) {
observeEvent(paste("list_num", 2, sep="")$value, { updateSliderInput(session, "val2", max=list_num$value)
updateNumericInput(session, "val22", max=list_num$value)

This may not be your best solution, but if you want to use text to build up an expression to be evaluated:

# we want to replicate this
iris$Sepal.Length

# this will fail
paste0("ir", "is")$Sepal.Length

# this will work
eval(parse(text = paste0("ir", "is", "$Sepal.Length")))
1 Like

I recommend against using assign in your shiny app, I don't think I've come across a good usecase for that yet.
Look at this recent example I made for another user.

1 Like

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.