Debian Codegraph @

R/environment_shadow.r

raw symbols SCIP
1 # Everthing related to the environment which takes functions which shadow base R functions.
2 # This is needed to build in our own needs, like properly shutting down the kernel
3 # when `quit()` is called.
4
5 add_to_user_searchpath <- function(name, FN, pkg = NULL) {
6 pkg_avail <- !is.null(pkg) && requireNamespace(pkg, quietly = TRUE)
7 if (pkg_avail) {
8 replace_in_package(pkg, name, FN)
9 } else {
10 assign(name, FN, 'jupyter:irkernel')
11 }
12 }
13
14 replace_in_package <- function(pkg, name, FN) {
15 env_name <- paste0('package:', pkg)
16 if (env_name %in% search())
17 replace_in_env(name, FN, as.environment(env_name))
18 replace_in_env(name, FN, getNamespace(pkg))
19 }
20
21 replace_in_env <- function(name, FN, env) {
22 .BaseNamespaceEnv$unlockBinding(name, env)
23 assign(name, FN, env)
24 .BaseNamespaceEnv$lockBinding(name, env)
25 }
26
27 get_shadowenv <- function() {
28 as.environment('jupyter:irkernel')
29 }
30
31 # save functions that are later replaced (called in .onLoad)
32 backup_env <- new.env()
33 # Circumvent windows build bug, see issue #530
34 backup_env$utils_flush_console <- function(...) {}
35 # Circumvent devtools bug
36 backup_env$base_flush_connection <- function(...) {}
37
38 init_backup_env <- function() {
39 if (!identical(environment(utils::flush.console), environment(utils::read.delim))) {
40 tb <- .traceback(2)
41 warning(
42 'init_backup_env called a second time after init_shadowenv:\n',
43 paste(capture.output(traceback(tb)), collapse = '\n')
44 )
45 return()
46 }
47 backup_env$base_flush_connection <- base::flush.connection
48 backup_env$utils_flush_console <- utils::flush.console
49 backup_env$base_quit <- base::quit
50 }
51
52 # Adds functions which do not need any access to the executer into the users searchpath
53 #' @importFrom utils getFromNamespace getS3method
54 #' @importFrom evaluate flush_console
55 init_shadowenv <- function() {
56 # add the accessors to the shadow env itself, so they are actually accessable
57 # from everywhere...
58 add_to_user_searchpath('.irk.get_shadowenv', get_shadowenv)
59 add_to_user_searchpath('.irk.add_to_user_searchpath', add_to_user_searchpath)
60
61 # For the rest of the functions, please explain why the workaround is needed
62 # (=the problem) and link to the issue describing the problem.
63
64 # workaround for problems with vignette(xxx) not bringing up the vignette
65 # content in the browser: https://github.com/IRkernel/IRkernel/issues/267
66 add_to_user_searchpath('print.vignette', function(x, ...) {
67 # R CMD check does not like us using :::
68 getS3method('print', 'vignette')(x, ...)
69 # returning immediately will run into trouble with zmq and its polling
70 # preventing the vignette server to startup. So wait a little to let
71 # it startup...
72 # 0.1 is too little, so add some margin...
73 Sys.sleep(0.5)
74 })
75
76 add_to_user_searchpath('View', function(x, title) {
77 if (!missing(title)) IRdisplay::display_text(title)
78 IRdisplay::display(x)
79 invisible(x) # the manpage says it returns NULL, but this is useful for piping
80 })
81 # we simply have currently no way to edit dfs:
82 # https://github.com/IRkernel/IRkernel/issues/280
83 add_to_user_searchpath('edit', function(...) {
84 stop(sQuote('edit()'), ' not yet supported in the Jupyter R kernel')
85 })
86
87 # stream output in loops:
88 # https://github.com/IRkernel/IRkernel/issues/3
89 replace_in_package('base', 'flush.connection', function(con) {
90 backup_env$base_flush_connection(con)
91 flush_console()
92 })
93 replace_in_package('utils', 'flush.console', function() {
94 backup_env$utils_flush_console()
95 flush_console()
96 })
97 }
98
99
100 init_cran_repo <- function() {
101 r <- getOption('repos')
102 is_unuseable_mirror <- identical(r, c(CRAN = '@CRAN@'))
103 if (is_unuseable_mirror) {
104 # the default repo according to https://cran.R-project.org/mirrors.html
105 # uses geo-redirects
106 r[['CRAN']] <- 'https://cran.r-project.org'
107 # attribute indicating the repos was set by us...
108 attr(r, 'irkernel') <- TRUE
109 options(repos = r)
110 }
111 }
112
113 init_session <- function() {
114 init_cran_repo()
115 # We support color even if isatty(stdout()) is FALSE
116 options(crayon.enabled = TRUE)
117 }
118
119
120 #' @importFrom grDevices pdf png
121 init_null_device <- function() {
122 # if possible, use a device that
123 # 1. prints no warnings for unicode (unlike pdf/postscript)
124 # 2. can handle /dev/null (unlike OSX devices)
125 # since there is nothing like that on OSX AFAIK, use pdf there (accepting warnings).
126
127 os <- get_os()
128
129 ok_device <- switch(os, win = png, osx = pdf, unix = png)
130 null_filename <- switch(os, win = 'NUL', osx = NULL, unix = '/dev/null')
131
132 null_device <- function(filename = null_filename, ...) ok_device(filename, ...)
133
134 if (identical(getOption('device'), pdf)) {
135 options(device = null_device)
136 }
137 }