Skip to content

Active bindings turned into constants during package build #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mllg opened this issue Aug 26, 2018 · 8 comments
Closed

Active bindings turned into constants during package build #152

mllg opened this issue Aug 26, 2018 · 8 comments

Comments

@mllg
Copy link
Member

mllg commented Aug 26, 2018

Here is a small package to illustrate the issue:

https://github.com/mllg/r6pkg

Everything works out fine while loading the package interactively (using devtools' load_all()):

load_all()
print(bar)
<foo>
  Public:
    clone: function (deep = FALSE)
    x: 10
    x_squared: active binding

The active binding is lost after installing and loading the package in a fresh R session via library():

library(r6pkg)
print(bar)
<foo>
  Public:
    clone: function (deep = FALSE)
    x: 10
    x_squared: 100

Looks like the binding gets evaluated during R CMD build and the returned value is stored instead.

@wch
Copy link
Member

wch commented Aug 27, 2018

It looks like this is what R does in packages: active bindings get saved as regular values.

If you add this to your package, the same thing happens:

#' @export
e <- new.env()
makeActiveBinding("x", function() runif(1), e)

Then after installing the package:

e$x
#> [1] 0.1425588
e$x
#> [1] 0.1425588
bindingIsActive("x", e)
#> [1] FALSE

Perhaps it's worth asking about on the R-devel mailing list?

@mllg
Copy link
Member Author

mllg commented Aug 28, 2018

Can confirm this one. I've updated the package to also include your code.

> library(r6pkg)
> unique(replicate(10, r6$x))
[1] 0.5698038
> unique(replicate(10, env$x))
[1] 0.262672

@kalibera, is this related to the bytecode compiler?

@mllg
Copy link
Member Author

mllg commented Aug 28, 2018

NB: saveRDS() + loadRDS() is not affected.

@kalibera
Copy link

Active bindings are preserved on serialization/deserialization, but one has to serialize them as bindings (not values) - so one has to serialize at least the environment they are part of.

@kalibera
Copy link

Active bindings are however not preserved when a package is being prepared for lazy loading (during package installation). The lazy loading database is already created with concrete values for active bindings.

@mllg
Copy link
Member Author

mllg commented Aug 28, 2018

So this is a feature, not a bug? 😕

@mllg
Copy link
Member Author

mllg commented Aug 29, 2018

Workaround: construct objects with active bindings in .onLoad().

@mllg mllg closed this as completed Aug 29, 2018
@kalibera
Copy link

It is probably unintentional, but may not be trivial to fix (could impact performance of package installation, would require some change in lazy loading database format). So if you can use .onLoad that would be great and would also work for released versions of R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants