r_session {callr}R Documentation

External R Session

Description

A permanent R session that runs in the background. This is an R6 class that extends the processx::process class.

Details

The process is started at the creation of the object, and then it can be used to evaluate R function calls, one at a time.

Usage

rs <- r_session$new(options = r_session_options(), wait = TRUE,
                     wait_timeout = 3000)

rs$run(func, args = list())
rs$run_with_output(func, args = list())
rs$call(func, args = list())

rs$poll_process(timeout)

rs$get_state()
rs$get_running_time()

rs$read()
rs$close(grace = 1000)

rs$traceback()
rs$debug()
rs$attach()

Arguments

Details

r_session$new() creates a new R background process. It can wait for the process to start up (wait = TRUE), or return immediately, i.e. before the process is actually ready to run. In the latter case you may call rs$poll_process() to make sure it is ready.

rs$run() is similar to r(), but runs the function in the rs R session. It throws an error if the function call generated an error in the child process.

rs$run_with_output() is similar to $run(), but returns the standard output and error of the child process as well. It does not throw on errors, but returns a non-zero error member in the result list.

rs$call() starts running a function in the background R session, and returns immediately. To check if the function is done, call the poll_process() method.

rs$poll_process() polls the R session with a timeout. If the session has finished the computation, it returns with "ready". If the timeout is reached, it returns with "timeout".

rs$get_state() return the state of the R session. Possible values:

rs$get_running_time() returns the elapsed time since the R process has started, and the elapsed time since the current computation has started. The latter is NA if there is no active computation.

rs$read() reads an event from the child process, if there is one available. Events might signal that the function call has finished, or they can be progress report events.

rs$close() terminates the current computation and the R process. The session object will be in "finished" state after this.

'rs$traceback() can be used after an error in the R subprocess. It is equivalent to the traceback() call, but it is performed in the subprocess.

rs$debug() is an interactive debugger to inspect the dumped frames in the subprocess, after an error. See more at r_session_debug.

rs$attach() is an experimental function that provides a REPL (Read-Eval-Print-Loop) to the subprocess.

Examples

## Not run: 
rs <- r_ression$new()

rs$run(function() 1 + 2)

rs$call(function() Sys.sleep(1))
rs$get_state()

rs$poll_process(-1)
rs$get_state()
rs$read()

## End(Not run)

[Package callr version 3.3.1 Index]