1
2#' External `R CMD` Process
3#'
4#' @description
5#' An `R CMD *` command that runs in the background. This is an R6 class
6#' that extends the [processx::process] class.
7#'
8#' @examplesIf FALSE
9#' options <- rcmd_process_options(cmd = "config", cmdargs = "CC")
10#' rp <- rcmd_process$new(options)
11#' rp$wait()
12#' rp$read_output_lines()
13#' @export
14
15rcmd_process <- R6::R6Class(
16  "rcmd_process",
17  inherit = processx::process,
18  public = list(
19    #' @description
20    #' Start an `R CMD` process.
21    #' @param options A list of options created via
22    #'  [rcmd_process_options()].
23    #' @return A new `rcmd_process` object.
24    initialize = function(options)
25      rcmdp_init(self, private, super, options),
26    #' @description
27    #' Clean up the temporary files created for an `R CMD` process.
28    finalize = function() {
29      unlink(private$options$tmp_files, recursive = TRUE)
30      if ("finalize" %in% ls(super)) super$finalize()
31    }
32  ),
33  private = list(
34    options = NULL
35  )
36)
37
38rcmdp_init <- function(self, private, super, options) {
39
40  ## This contains the context that we set up in steps
41  options <- convert_and_check_my_args(options)
42
43  options <- setup_context(options)
44  options <- setup_rcmd_binary_and_args(options)
45
46  private$options <- options
47
48  oldwd <- getwd()
49  setwd(options$wd)
50  on.exit(setwd(oldwd), add = TRUE)
51
52  with_envvar(
53    options$env,
54    do.call(super$initialize, c(list(options$bin, options$real_cmdargs,
55      stdout = options$stdout, stderr = options$stderr,
56      poll_connection = options$poll_connection),
57      options$extra))
58  )
59
60  invisible(self)
61}
62