| 1 |
|
| 2 |
#' Run a script |
| 3 |
#' |
| 4 |
#' Run an \R script, in the context of a project using renv. The script will |
| 5 |
#' be run within an \R sub-process. |
| 6 |
#' |
| 7 |
#' @inherit renv-params |
| 8 |
#' |
| 9 |
#' @param script The path to an \R script. |
| 10 |
#' |
| 11 |
#' @param job Run the requested script as an RStudio job? Requires a recent |
| 12 |
#' version of both RStudio and the rstudioapi packages. When `NULL`, the |
| 13 |
#' script will be run as a job if possible, and as a regular \R process |
| 14 |
#' launched by [system2()] if not. |
| 15 |
#' |
| 16 |
#' @param name The name to associate with the job, for scripts run as a job. |
| 17 |
#' |
| 18 |
#' @param args description A character vector of command line arguments to be |
| 19 |
#' passed to the launched job. These parameters can be accessed via |
| 20 |
#' `commandArgs(trailingOnly = FALSE)`. |
| 21 |
#' |
| 22 |
#' @param project The path to the renv project. This project will be loaded |
| 23 |
#' before the requested script is executed. When `NULL` (the default), renv |
| 24 |
#' will automatically determine the project root for the associated script |
| 25 |
#' if possible. |
| 26 |
#' |
| 27 |
#' @export |
| 28 |
run <- function(script, |
| 29 |
..., |
| 30 |
job = NULL, |
| 31 |
name = NULL, |
| 32 |
args = NULL, |
| 33 |
project = NULL) |
| 34 |
{ |
| 35 |
renv_scope_error_handler() |
| 36 |
renv_dots_check(...) |
| 37 |
|
| 38 |
script <- renv_path_normalize(script, mustWork = TRUE) |
| 39 |
|
| 40 |
# find the project directory |
| 41 |
project <- project %||% renv_file_find(script, function(path) { |
| 42 |
paths <- file.path(path, c("renv", "renv.lock")) |
| 43 |
if (any(file.exists(paths))) |
| 44 |
return(path) |
| 45 |
}) |
| 46 |
|
| 47 |
if (is.null(project)) { |
| 48 |
fmt <- "could not determine project root for script '%s'" |
| 49 |
stopf(fmt, renv_path_aliased(script)) |
| 50 |
} |
| 51 |
|
| 52 |
# ensure that it has an activate script |
| 53 |
activate <- renv_paths_activate(project = project) |
| 54 |
if (!file.exists(activate)) { |
| 55 |
fmt <- "project '%s' does not have an renv activate script" |
| 56 |
stopf(fmt, renv_path_aliased(project)) |
| 57 |
} |
| 58 |
|
| 59 |
# run as a job when possible in RStudio |
| 60 |
jobbable <- |
| 61 |
!identical(job, FALSE) && |
| 62 |
renv_rstudio_available() && |
| 63 |
renv_package_installed("rstudioapi") && |
| 64 |
renv_package_version("rstudioapi") >= "0.10" && |
| 65 |
rstudioapi::verifyAvailable("1.2.1335") |
| 66 |
|
| 67 |
if (identical(job, TRUE) && identical(jobbable, FALSE)) |
| 68 |
stopf("cannot run script as job: required versions of RStudio + rstudioapi not available") |
| 69 |
|
| 70 |
if (jobbable) |
| 71 |
renv_run_job(script = script, name = name, args = args, project = project) |
| 72 |
else |
| 73 |
renv_run_impl(script = script, name = name, args = args, project = project) |
| 74 |
} |
| 75 |
|
| 76 |
renv_run_job <- function(script, name, args, project) { |
| 77 |
|
| 78 |
activate <- renv_paths_activate(project = project) |
| 79 |
exprs <- expr({ |
| 80 |
|
| 81 |
# insert a shim for commandArgs |
| 82 |
local({ |
| 83 |
|
| 84 |
# unlock binding temporarily |
| 85 |
base <- .BaseNamespaceEnv |
| 86 |
base$unlockBinding("commandArgs", base) |
| 87 |
on.exit(base$lockBinding("commandArgs", base), add = TRUE) |
| 88 |
|
| 89 |
# insert our shim |
| 90 |
cargs <- commandArgs(trailingOnly = FALSE) |
| 91 |
base$commandArgs <- function(trailingOnly = FALSE) { |
| 92 |
result <- !!args |
| 93 |
if (trailingOnly) result else union(cargs, result) |
| 94 |
} |
| 95 |
|
| 96 |
}) |
| 97 |
|
| 98 |
# run the script |
| 99 |
source(!!activate) |
| 100 |
source(!!script) |
| 101 |
|
| 102 |
}) |
| 103 |
|
| 104 |
code <- deparse(exprs) |
| 105 |
jobscript <- tempfile("renv-job-", fileext = ".R") |
| 106 |
writeLines(code, con = jobscript) |
| 107 |
|
| 108 |
rstudioapi::jobRunScript( |
| 109 |
path = jobscript, |
| 110 |
workingDir = project, |
| 111 |
name = name |
| 112 |
) |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
renv_run_impl <- function(script, name, args, project) { |
| 117 |
|
| 118 |
renv_scope_wd(project) |
| 119 |
|
| 120 |
stdout <- if (testing()) FALSE else "" |
| 121 |
stderr <- if (testing()) FALSE else "" |
| 122 |
|
| 123 |
args <- c( |
| 124 |
"-s", "-f", renv_shell_path(script), |
| 125 |
if (length(args)) c("--args", args) |
| 126 |
) |
| 127 |
|
| 128 |
system2( |
| 129 |
command = R(), |
| 130 |
args = args, |
| 131 |
stdout = stdout, |
| 132 |
stderr = stderr, |
| 133 |
wait = FALSE |
| 134 |
) |
| 135 |
|
| 136 |
} |