-
Notifications
You must be signed in to change notification settings - Fork 54
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
Initiatialising a variable on package load #128
Comments
Another way is to hook assignment like this: on_load(
var <- value
) This uses the hook implemented in https://github.com/r-lib/rlang/blob/master/R/aaa.R (could be made a base-only compat file and I'm planning to export from rlang at some point). The big advantage besides readability is that you can put the assignment in context rather than in |
I think that we might now favour # in R/drive_auth.R
the <- new.env(parent = empty_env()
# in R/zzz.R
.onLoad <- function(libname, pkgname) {
the$auth_state <- gargle::init_AuthState(
package = "googledrive",
auth_active = TRUE
)
...
} |
As @lionel- said, it is useful to have the |
Ah yeah, you can't generally just do |
FWIW, after tracking down a bug that involved debugging |
If use and justification of |
If I'm not mistaken you're referring to having to grepp for How about having an envvar that turns on printing of the entire set of on-load expressions that are about to be run, as well as some information between each invokation. I think this would improve the debugging experience compared to |
We could reuse |
Grepping is fine, but I wanted to put a |
I think you can do that, though that might require adding curly braces, e.g. |
I wanted to step over all the stuff that happens in
and the actual code is in Something like this. There is probably a more efficient way of doing this.... |
I see, thanks. It would probably take a morning or so to implement something like in coro where we generate browsable code. Using it would look like browsing around all the files that call Edit: ah if you insert a |
Ah, no need, I'll just get better at this... :D |
A non-rlang way to do this is to define helper functions like https://github.com/RConsortium/S7/blob/main/R/zzz.R#L121-L128 For use in the broader community, I think I favour that technique because it's trivial to understand. I'll still mention |
The callout might mention that the on_load(
foo <- bar()
) versus foo <- NULL
on_load_my_file <- function() {
foo <<- bar()
} |
Summarizing a recent Slack discussion that seems worth preserving.
The conversation was triggered by r-lib/gargle#184, where someone following a pattern seen in several gargle-using packages was getting push back from CRAN for a new package submission.
Specifically, CRAN objected to the use of
<<-
in.onLoad()
, interpreting that as modifying the global environment.As it turns out, this object was created elsewhere in the package, in the package's namespace, so the
<<-
was modifying that object.But this use of
<<-
is not easily seen to be OK, so it's worth having better patterns for this.Typical "before" code, that might get flagged by CRAN:
The objective in this case is to make sure that googledrive's internal
.auth
object is an instance of gargle's R6 classAuthState
, according to the current, locally installed version of gargle, not the ambient gargle version when the googledrive binary was built.Discussion with @jimhester @gaborcsardi @jeroen @DavisVaughan lead to these observations and alternatives:
Put the
.auth <- NULL
assignment as close as possible to the<<-
, so it's easier to see what's going on and that the super assignment does not touch global env.Use
utils::assignInMyNamespace()
to make it clear that the target object lives in my namespace. You still must create the target object elsewhere, asutils::assignInMyNamespace()
can only modify, not create.Use
assign()
and specify the environment asenvironment(.onLoad)
,topenv()
, orasNamespace("pkg")
. Note that the target.auth
object does not need to be pre-created in this case.The text was updated successfully, but these errors were encountered: