1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
test_py_require_reset <- function() {
.globals$python_requirements <- NULL
}
r_session <- function(exprs, echo = TRUE, color = FALSE,
attach_namespace = FALSE) {
exprs <- substitute(exprs)
if (!is.call(exprs))
stop("exprs must be a call")
exprs <- if (identical(exprs[[1]], quote(`{`)))
as.list(exprs)[-1]
else
list(exprs)
exprs <- unlist(c(
if (attach_namespace)
'attach(asNamespace("reticulate"), name = "namespace:reticulate", warn.conflicts = FALSE)',
if (echo)
"options(echo = TRUE)",
lapply(exprs, deparse)
))
writeLines(exprs, file <- tempfile(fileext = ".R"))
on.exit(unlink(file), add = TRUE)
result <- suppressWarnings(system2(
R.home("bin/R"),
c("--quiet", "--no-save", "--no-restore", "--no-echo", "-f", file),
stdout = TRUE, stderr = TRUE,
env = c(character(), if (isFALSE(color)) "NO_COLOR=1")
))
class(result) <- "r_session_record"
result
}
print.r_session_record <- function(record, echo = TRUE) {
writeLines(record)
status <- attr(record, "status", TRUE)
cat(sep = "",
"------- session end -------\n",
"success: ", if (is.null(status)) "true" else "false", "\n",
"exit_code: ", status %||% 0L, "\n")
}
registerS3method("print", "r_session_record", print.r_session_record,
envir = environment(print))
py_require_tested_packages <- function() {
py_require(c(
"docutils", "pandas", "scipy", "matplotlib", "ipython",
"tabulate", "plotly", "psutil", "kaleido", "wrapt"
))
}
py_require_tested_packages()
|