I have been trying to store an activeBinding inside a list in the following way
set.seed(1)
makeActiveBinding("independent_var", purrr::partial(runif, n=1), env=environment())
independent_var
# [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)
active_elements$active_element
# [1] 0.3721239
active_elements$active_element
# [1] 0.3721239
##### What I would like: ######
active_elements$active_element
# [1] 0.5728534
active_elements$active_element
# [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
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.
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:
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
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:
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.
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