Page 1 of 1

Jupyter IRkernel compatibility

Posted: 17 Aug 2022, 14:14
by kathoef
A colleague is preparing course materials for the upcoming semester, in which Apollo amongst other things is used with Jupyter notebooks and the R Jupyter kernel (https://github.com/IRkernel/IRkernel) on a JupyterHub system running at our university. Therein, he was experiencing problems with a dying Jupyter kernel. The server logs say:
[D 2022-08-17 13:20:55.100 ServerApp] Accepting token-authenticated connection from 172.17.0.1
[D 2022-08-17 13:20:55.101 ServerApp] 200 GET /api/kernels?1660735255082 (172.17.0.1) 1.47ms
Error in as.environment(pos) :
no item called "jupyter:irkernel" on the search list
Calls: <Anonymous> ... handle_shell -> <Anonymous> -> add_to_user_searchpath -> assign
In addition: Warning message:
In sink() : no sink to remove
[D 2022-08-17 13:20:55.726 ServerApp] activity on 327c5e71-f71f-444d-897e-78c677061646: status (busy)
Execution halted
Error in detach("jupyter:irkernel") : invalid 'name' argument
Calls: <Anonymous> -> <Anonymous> -> detach
[D 2022-08-17 13:20:55.727 ServerApp] activity on 327c5e71-f71f-444d-897e-78c677061646: execute_input
[D 2022-08-17 13:20:55.769 ServerApp] 304 GET /static/favicons/favicon-busy-1.ico (172.17.0.1) 1.07ms
[D 2022-08-17 13:20:55.856 ServerApp] Accepting token-authenticated connection from 172.17.0.1
The code works well in R Studio Server and also from the R console, but not from the R Jupyter kernel. All of the tests were done in the same Docker container environment, so there should be no impact from e.g. software enviroment version differences.

I think, I was able to pin down the problem to the apollo_initialise() function,

Code: Select all

apollo_initialise <- function()
{
  doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
  doDetach <- (search())[which(doDetach)]
  if(length(doDetach)>0) for(i in 1:length(doDetach)) detach(pos=(which(doDetach[i]==search()))[1])
  if(sink.number()>0) sink()
  cat("Apollo ignition sequence completed\n")
}
which apparently alters a few R environment things after R startup.

From using the R command line,

Code: Select all

$ R

R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

> library("apollo")
Apollo 0.2.7
http://www.ApolloChoiceModelling.com
See url for a detailed manual, examples and a user forum.
Sign up to to user forum to receive updates on new releases.

Your version of Apollo is more than six months old.
Using the latest version will ensure you have all
 current functionality and bug fixes.
You can update to the latest version by typing:
 install.packages("apollo")
> doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
> doDetach <- (search())[which(doDetach)]
> doDetach
[1] "org:r-lib"
> search()
 [1] ".GlobalEnv"        "package:apollo"    "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "org:r-lib"         "package:base"
and from using the R Jupyter kernel,

Code: Select all

[1] library("apollo")
Apollo 0.2.7
http://www.ApolloChoiceModelling.com
See url for a detailed manual, examples and a user forum.
Sign up to to user forum to receive updates on new releases.

Your version of Apollo is more than six months old.
Using the latest version will ensure you have all
 current functionality and bug fixes.
You can update to the latest version by typing:
 install.packages("apollo")
[2] doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
[3] doDetach <- (search())[which(doDetach)]
[4] doDetach
'jupyter:irkernel'
[5] search()
'.GlobalEnv''package:apollo''jupyter:irkernel''package:stats''package:graphics''package:grDevices''package:utils''package:datasets''package:methods''Autoloads''package:base'
one can see that by using the apollo_initialise() function, the "jupyter:irkernel" package gets detached, which I assume then causes the R Jupyter kernel death. For the sake of completeness, the R Jupyter kernel is started in the following way

Code: Select all

$ cat /usr/local/share/jupyter/kernels/ir/kernel.json 
{
  "argv": ["/usr/lib/R/bin/R", "--slave", "-e", "IRkernel::main()", "--args", "{connection_file}"],
  "display_name": "R",
  "language": "R"
}
I assume that changing the line

Code: Select all

doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
to something like

Code: Select all

doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv|jupyter:irkernel)", search())
in the apollo_initialise() function would fix the problem, but since I am neither an R nor an Apollo user myself, I do not know about the implications. As a workaround, I will suggest to my colleague to not use the apollo_initialise() function in Jupyter notebooks. But of course, it would be really great, if we could soon install from an Apollo package version, in which this problem is fixed.

Re: Jupyter IRkernel compatibility

Posted: 25 Nov 2022, 16:53
by dpalma
Hi,

Sorry for the long delay in replying.

We usually work on R through the RStudio IDE, so I am afraid we have no experience with Jupyter notebooks. So we very much appreciate your insight on this.

Indeed it looks like apollo_initialise is the problem. We will test your proposal for a future version of Apollo. For now, you can simply skip the call to apollo_initialise. That function is only there as a safeguard in case the user forgot to close a sink(), in which case they would not see any output to the console; or if there was a very dramatic interruption of the estimation process, which it really shouldn't happen. So I would say that you can safely skip the call to apollo_initialise.

Best wishes
David