R: Vectorise setting active fields in R6

117 Views Asked by At

I have a simple R6 object generator, with a get/set active binding:

myClass <- R6Class("myClass",
                   private = list(
                     ..status = TRUE
                   ),
                   active = list(
                     status = function(value) {
                       if (missing(value)) {
                         private$..status
                       } else private$..status <- value
                     }
                   )
)

I can create a list of objects, generated using myClass:

class_list <- map(1:10, ~ myClass$new())

I can get the status of each object in the list, using purrr:: map:

map(class_list, ~ .x$status)

But what if I want to set the status of all objects in class_list? I wanted to do this with purrr:: walk:

walk(class_list, ~ .x$status <- FALSE)

This throws an Error: unexpected [symbol]..., as does using =.

Can anyone offer a solution? Using purrr is preferable.

Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

We can wrap those assignments within the {} and return the original object (.x)

map(class_list, ~ {.x$status <- FALSE; .x})

Now, check the status again

map_lgl(class_list, ~ .x$status)
#[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE