Storing an active binding inside a list

Hi all,

I have been trying to store an activeBinding inside a list in the following way

makeActiveBinding("independent_var", purrr::partial(runif, n=1), env=environment())
# [1] 0.2655087
# All OK!

# Here the element is evaluated. So inside I have a fix number not the active binding itself.
active_elements <- list("active_element" = independent_var)
# [1] 0.3721239

# [1] 0.3721239

##### What I would like: ######
# [1] 0.5728534

# [1] 0.9082078

Of course that doesn't work because the evaluation happens when I bind the variable to list element, and in fact it would be quite unintuitive if it worked the way I tried :slight_smile:

Is there any way to achieve that, I know you can do it with environments, but I was wondering if it was possible within lists.

Thanks a lot for your help!

Are you sure this is a good idea to pursue ?
Speaking for myself, I wouldn't expect a named object to return different values when I refer to it, I might consider that possibility if I was running a named function of course. Is there really a scenario where muddying the waters on whether a name is a value or a function call brings benefits ?

i.e. I'm saying this looks better to me on its face:

active_elements2 <- list("active_element" = purrr::partial(runif, n=1))


Thanks a lot for your response! I agree with you completely, that in most cases this is a bad idea. Nonetheless, there is a still few cases, in which this is a good idea.

Just for a bit of background. I have several variables inside a shiny application that can be accessed without explicitly calling a function on them and I have one where the call must be explicit. All of this happens inside a small framework I am developing, so I would like the calling of all this variables/functions to be uniform. For the framework user all these variables are equivalent, although under the hood they are quite different.

I admit my is question closer to "can this be done" and not "should I be doing this", as I have already solved my issue another way :slight_smile:

Thanks again!

well, environments are lists with other constraints on top.. so this has the same form vis how you would access it, i.e. it fits the particular pattern you displayed you were trying to make:

active_elements <- rlang::new_environment()
makeActiveBinding(sym = "active_element",fun = purrr::partial(runif, n=1),env = active_elements)

so I've dodged 'active_elements' should be a list requirement , by having it be an environment. but its accessed with the same syntax in the way you showed you wanted to access it potentially.

First of all, thanks for your hint!

It is true that it can be done with environments, and the similarity in how to access environments and lists makes it almost the same from outside. But I am specifically curious in knowing if anything within R prevents this from doing it inside a list or it can only be done with environments :slight_smile:

Thanks again!

This topic was automatically closed 21 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.