Debian Codegraph @

R/installspec.r

raw symbols SCIP
1 #' Install the kernelspec to tell Jupyter about IRkernel.
2 #'
3 #' This can be called multiple times for different R interpreter, but you have to give a
4 #' different name (and displayname to see a difference in the notebook UI). If the same
5 #' name is give, it will overwrite older versions of the kernel spec with that name!
6 #'
7 #' @param user Install into user directory (\href{https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html}{\code{$XDG_DATA_HOME}}\code{/jupyter/kernels}) or globally? (default: NULL but treated as TRUE if "prefix" is not specified)
8 #' @param name The name of the kernel (default "ir")
9 #' @param displayname The name which is displayed in the notebook (default: "R")
10 #' @param rprofile (optional) Path to kernel-specific Rprofile (defaults to system-level settings)
11 #' @param prefix (optional) Path to alternate directory to install kernelspec into (default: NULL)
12 #' @param sys_prefix (optional) Install kernelspec using the \code{--sys-prefix} option of the currently detected jupyter (default: NULL)
13 #' @param verbose (optional) If \code{FALSE}, silence output of \code{install}
14 #' @param env (optional) Named list of environment variables to set in the kernel (default: NULL)
15 #'
16 #' @return Exit code of the \code{jupyter kernelspec install} call.
17 #'
18 #' @export
19 installspec <- function(
20 user = NULL, name = 'ir', displayname = 'R', rprofile = NULL, prefix = NULL, sys_prefix = NULL, verbose = getOption('verbose'), env = NULL
21 ) {
22 exit_code <- system2('jupyter', c('kernelspec', '--version'), FALSE, FALSE)
23 if (exit_code != 0)
24 stop('jupyter-client has to be installed but ', dQuote('jupyter kernelspec --version'), ' exited with code ', exit_code, '.\n')
25
26 # default to 'user' install if neither 'user' or 'prefix' is specified
27 if (is.null(user)) user <- (is.null(prefix) && is.null(sys_prefix))
28 if (sum(user, !is.null(prefix), !is.null(sys_prefix)) > 1)
29 stop('"user", "prefix", "sys_prefix" are mutually exclusive')
30
31 # make a kernelspec with the current interpreter's absolute path
32 srcdir <- system.file('kernelspec', package = 'IRkernel')
33 tmp_name <- tempfile()
34 dir.create(tmp_name)
35 file.copy(srcdir, tmp_name, recursive = TRUE)
36 spec_path <- file.path(tmp_name, 'kernelspec', 'kernel.json')
37 spec <- fromJSON(spec_path)
38 spec$argv[[1]] <- file.path(R.home('bin'), 'R')
39 spec$display_name <- displayname
40
41 spec$env <- if (!is.null(env)) env else namedlist()
42 if (!is.list(spec$env) || is.null(names(spec$env)))
43 stop('`env` needs to be a named list')
44 if (!is.null(rprofile))
45 spec$env$R_PROFILE_USER <- rprofile
46
47 write(toJSON(spec, pretty = TRUE, auto_unbox = TRUE), file = spec_path)
48
49 user_flag <- if (user) '--user' else character(0)
50 prefix_flag <- if (!is.null(prefix)) c('--prefix', prefix) else character(0)
51 sys_prefix_flag <- if (!is.null(sys_prefix)) c('--sys-prefix', prefix) else character(0)
52 quiet_flag <- if (!verbose) '--log-level=WARN' else character(0)
53 args <- c('kernelspec', 'install', '--replace', '--name', name, user_flag, prefix_flag, sys_prefix_flag, quiet_flag, file.path(tmp_name, 'kernelspec'))
54 exit_code <- system2('jupyter', args)
55
56 unlink(tmp_name, recursive = TRUE)
57
58 invisible(exit_code)
59 }