access nested reactiveValues object

Hi I create a reactiveValues from a nested list

rv  = do.call(shiny::reactiveValues,list(a1=list(b1 = "b1",b2 = "b2"), a2 = "a2"))

which can be accessed easily with

isolate(rv[["a1"]][["b1"]]) # works, returns "b1"

Problem
To access a reactiveValues with character vector doesn't work.

isolate(rv[[c("a1","b1")]]) # Error: Must use single string to index into reactivevalues.

I would like to create a function which allows to access the nested reactiveValues generically:

#' returns value at index
#'
#' @param rv reactiveValues()
#' @param field character vector
#'
#' @return value at index
access_rv = function(rv,field){
  # ...
}

I think I solved it myself:

#' returns value at index
#'
#' @param rv reactiveValues()
#' @param field character vector
#'
#' @return value at index
access_rv = function(rv,field){
  isolate(purrr::pluck(rv, !!!field))
}

access_rv(rv,c("a1","b1")) # works, return "b1"

and also

set_rv = function(rv,field,value){
  isolate(purrr::pluck(rv, !!!field) <- value )
}
set_rv(rv,c("a1","b1"),"c1") # works
access_rv(rv,c("a1","b1")) # works, return "c1"

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.