R/enslave.R

Defines functions enslave

Documented in enslave

#' Run a selection as an RStudio background job
#'
#' @export
enslave <- function() {

  ctx <- rstudioapi::getActiveDocumentContext()

  if (!is.null(ctx)) {

    if (ctx$selection[[1]]$text != "") {

      bits <- ctx$selection[[1]]$text

      tf <- tempfile(fileext = ".R")
      writeLines(bits, con = tf)

      nm <- sprintf("enslaved_%s", gsub("\\-", "", uuid::UUIDgenerate()))

      .rs.api.runScriptJob(
        path = tf,
        workingDir = getwd(),
        importEnv = TRUE,
        exportEnv = nm
      )

      message("Job started. Results will be in ", nm)

    }

  }

}
hrbrmstr/hrbraddins documentation built on Oct. 20, 2019, 7:43 a.m.