runbg: Run an R expression in the background (only on UNIX)

View source: R/remoteComputing.R

runbgR Documentation

Run an R expression in the background (only on UNIX)

Description

Generate an R code of the expression that is copied via scp to any machine (ssh-key needed). Then collect the results.

Usage

runbg(
  ...,
  machine = "localhost",
  filename = NULL,
  input = ls(.GlobalEnv),
  compile = FALSE,
  wait = FALSE,
  recover = F
)

Arguments

...

Some R code

machine

Character vector, e.g. "localhost" or "knecht1.fdm.uni-freiburg.de" or c(localhost, localhost).

filename

Character, defining the filename of the temporary file. Random file name ist chosen if NULL.

input

Character vector, the objects in the workspace that are stored into an R data file and copied to the remove machine.

compile

Logical. If TRUE, C files are copied and compiled on the remote machine. Otherwise, the .so files are copied.

wait

Logical. Wait until executed. If TRUE, the code checks if the result file is already present in which case it is loaded. If not present, runbg() starts, produces the result and loads it as .runbgOutput directly into the workspace. If wait = FALSE, runbg() starts in the background and the result is only loaded into the workspace when the get() function is called, see Value section.

recover

Logical. This option is useful to recover the three functions check(), get() and purge(), e.g. when a session has crashed. Then, the three functions are recreated without restarting the job. They can then be used to get the results of a job wihtout having to do it manually. Requires the correct filename, so if the previous runbg was run with filename = NULL, you have to specify the tmp_filename manually.

Details

runbg() generates a workspace from the input argument and copies the workspace and all C files or .so files to the remote machines via scp. This will only work if *an ssh-key had been generated and added to the authorized keys on the remote machine*. The code snippet, i.e. the ... argument, can include several intermediate results but only the last call which is not redirected into a variable is returned via the variable .runbgOutput, see example below.

Value

List of functions check, get() and purge(). check() checks, if the result is ready. get() copies the result file to the working directory and loads it into the workspace as an object called .runbgOutput. This object is a list named according to the machines that contains the results returned by each machine. purge() deletes the temporary folder from the working directory and the remote machines.

Examples

## Not run: 
out_job1 <- runbg({
         M <- matrix(rnorm(1e2), 10, 10)
         solve(M)
         }, machine = c("localhost", "localhost"), filename = "job1")
out_job1$check()          
out_job1$get()
result <- .runbgOutput
print(result)
out_job1$purge()

## End(Not run)
## Not run: 
#' Recover a runbg job with the option "recover"
out_job1 <- runbg({
         M <- matrix(rnorm(1e2), 10, 10)
         solve(M)
         }, machine = c("localhost", "localhost"), filename = "job1")
Sys.sleep(1)
remove(out_job1)
try(out_job1$check())
out_job1 <- runbg({
  "This code is not run"
}, machine = c("localhost", "localhost"), filename = "job1", recover = T)
out_job1$get()
result <- .runbgOutput
print(result)
out_job1$purge()

## End(Not run)

dkaschek/dMod documentation built on July 27, 2023, 11:45 p.m.