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
Add special env to take r shadowing functions #324
Conversation
We need to get some functions into the search path so that they shadow functions of R proper. One examples is `quit()` which we have to implement so that it properly shuts down the kernel and tells the frontend that it wants to shutdown. After some experimentation, the environment had to be added in the main codepath so that a) other places in the code could access it and b) it was actually places into the searchpath between the already loaded packages and the .Globalenv.
|
on.exit({ | ||
detach("jupyter:irkernel") | ||
}) | ||
assign(".irk.get_userenv", function() {.irk.env}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where does this one end up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In .irk.env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it should, will fix...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
Latest code:
|
I think I have now created and reverted |
OK, what didn’t work?
after all, it would be best to have all the stuff in |
on.exit({ | ||
detach("jupyter:irkernel") | ||
}) | ||
assign(".irk.get_userenv", function() {.irk.env}, .irk.env) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming quibble: 'user environment' is a lot like 'user namespace', which in IPython means the namespace where user code executes (i.e. typing a = 1
defines a in the user namespace). That's not what we mean here. Can we come up with a clearer name? E.g. get_shadowing_env
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
What could work is
Will try that now... |
Ah, and that doesn't work because of
If someone wants to have a try, this is the best I can give you... :-/ For reference: # Everthing related to the environment which takes functions which shadow base R functions.
# This is needed to build in our own needs, like properly shutting down the kernel
# when `quit()` is called.
# The real env is created with attach in the main() via the call to init_shadowenv()!
.irk.shadowenv <- NULL
get_shadowenv <- function() {.irk.shadowenv}
add_to_user_searchpath <- function(name, FN, attrs = list()) {
if (is.null(.irk.shadowenv)) stop("ASSERT: shadowenv was NULL", call. = FALSE)
assign(name, FN, .irk.shadowenv)
}
init_shadowenv <- function(){
.irk.shadowenv <<- attach(NULL, name = "jupyter:irkernel")
# also add the accessors to the shadow env to that env
assign(".irk.get_shadowenv", get_shadowenv, .irk.shadowenv)
assign(".irk.add_to_user_searchpath", add_to_user_searchpath, .irk.shadowenv)
} and then in kernel.r -> init_shadowenv()
on.exit({
detach("jupyter:irkernel")
}) |
you missed my point about using the env name and not the env object. that will probably work |
Ok, I found a way to put it all into a function and call it from main... It's also not as nice, but at least all the code is in one place... |
4073d36
to
c770890
Compare
So, have a look and merge, I think this is ready... |
Seems reasonable. |
The env and the functions to access them have to be done in the function which is called from main() because: * Attaching it on the package level does not add it to the search path during evaluate * creating it on the package level and only attaching it in main() will copy the env and we can#t get a handle to that copied env * reassigning the env to the package level variable needs some trickery (it's locked) and that seems worse than doing it in a big function...
|
||
# The real env is created and attach'ed in the main() via the call to init_shadowenv()! | ||
init_shadowenv <- function(){ | ||
.irk.shadowenv <- attach(NULL, name = "jupyter:irkernel") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
){
→ ) {
""
→ ''
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should really invest some time into setting up some precommit hook...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i tried to create some lintr rules, but somehow can’t get it to run: r-lib/lintr#139
no idea what i do wrong
jup, looks good! some questions and style things remain. |
Refactored again: now the thing is out of main() and in the executor. |
Also use names instead of direct env references. All additional functions which should be laced into the shadow env need to be added in the `init_shadowenv()` function.
perfect! |
Closes: #301