| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 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 |
|
| 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 |
} |