% Generated by roxygen2: do not edit by hand % Please edit documentation in R/r-session.R \name{r_session} \alias{r_session} \title{External R Session} \description{ A permanent R session that runs in the background. This is an R6 class that extends the \link[processx:process]{processx::process} class. 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. } \examples{ \dontshow{if (FALSE) withAutoprint(\{ # examplesIf} rs <- r_session$new() rs$run(function() 1 + 2) rs$call(function() Sys.sleep(1)) rs$get_state() rs$poll_process(-1) rs$get_state() rs$read() \dontshow{\}) # examplesIf} } \section{Super class}{ \code{\link[processx:process]{processx::process}} -> \code{r_session} } \section{Public fields}{ \if{html}{\out{
}} \describe{ \item{\code{status}}{Status codes returned by \code{read()}.} } \if{html}{\out{
}} } \section{Methods}{ \subsection{Public methods}{ \itemize{ \item \href{#method-r_session-initialize}{\code{r_session$new()}} \item \href{#method-r_session-run}{\code{r_session$run()}} \item \href{#method-r_session-run_with_output}{\code{r_session$run_with_output()}} \item \href{#method-r_session-call}{\code{r_session$call()}} \item \href{#method-r_session-poll_process}{\code{r_session$poll_process()}} \item \href{#method-r_session-get_state}{\code{r_session$get_state()}} \item \href{#method-r_session-get_running_time}{\code{r_session$get_running_time()}} \item \href{#method-r_session-read}{\code{r_session$read()}} \item \href{#method-r_session-close}{\code{r_session$close()}} \item \href{#method-r_session-traceback}{\code{r_session$traceback()}} \item \href{#method-r_session-debug}{\code{r_session$debug()}} \item \href{#method-r_session-attach}{\code{r_session$attach()}} \item \href{#method-r_session-print}{\code{r_session$print()}} \item \href{#method-r_session-cleanup}{\code{r_session$cleanup()}} \item \href{#method-r_session-finalize}{\code{r_session$finalize()}} \item \href{#method-r_session-clone}{\code{r_session$clone()}} } } \if{html}{\out{
Inherited methods

- `processx::process$as_ps_handle()`
- `processx::process$format()`
- `processx::process$get_cmdline()`
- `processx::process$get_cpu_times()`
- `processx::process$get_end_time()`
- `processx::process$get_error_connection()`
- `processx::process$get_error_file()`
- `processx::process$get_exe()`
- `processx::process$get_exit_status()`
- `processx::process$get_input_connection()`
- `processx::process$get_input_file()`
- `processx::process$get_memory_info()`
- `processx::process$get_name()`
- `processx::process$get_output_connection()`
- `processx::process$get_output_file()`
- `processx::process$get_pid()`
- `processx::process$get_poll_connection()`
- `processx::process$get_result()`
- `processx::process$get_start_time()`
- `processx::process$get_status()`
- `processx::process$get_username()`
- `processx::process$get_wd()`
- `processx::process$has_error_connection()`
- `processx::process$has_input_connection()`
- `processx::process$has_output_connection()`
- `processx::process$has_poll_connection()`
- `processx::process$interrupt()`
- `processx::process$is_alive()`
- `processx::process$is_incomplete_error()`
- `processx::process$is_incomplete_output()`
- `processx::process$is_supervised()`
- `processx::process$kill()`
- `processx::process$kill_tree()`
- `processx::process$poll_io()`
- `processx::process$read_all_error()`
- `processx::process$read_all_error_lines()`
- `processx::process$read_all_output()`
- `processx::process$read_all_output_lines()`
- `processx::process$read_error()`
- `processx::process$read_error_bytes()`
- `processx::process$read_error_lines()`
- `processx::process$read_output()`
- `processx::process$read_output_bytes()`
- `processx::process$read_output_lines()`
- `processx::process$resume()`
- `processx::process$signal()`
- `processx::process$supervise()`
- `processx::process$suspend()`
- `processx::process$wait()`
- `processx::process$write_input()`

}} \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-initialize}{}}} \subsection{\code{r_session$new()}}{ creates a new R background process. It can wait for the process to start up (\code{wait = TRUE}), or return immediately, i.e. before the process is actually ready to run. In the latter case you may call the \code{poll_process()} method to make sure it is ready. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$new(options = r_session_options(), wait = TRUE, wait_timeout = 3000)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{options}}{A list of options created via \code{\link[=r_session_options]{r_session_options()}}.} \item{\code{wait}}{Whether to wait for the R process to start and be ready for running commands.} \item{\code{wait_timeout}}{Timeout for waiting for the R process to start, in milliseconds.} } \if{html}{\out{
}} } \subsection{Returns}{ An \code{r_session} object. } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-run}{}}} \subsection{\code{r_session$run()}}{ Similar to \code{\link[=r]{r()}}, but runs the function in a permanent background R session. It throws an error if the function call generated an error in the child process. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$run(func, args = list(), package = NULL)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{func}}{Function object to call in the background R process. Please read the notes for the similar argument of \code{\link[=r]{r()}}.} \item{\code{args}}{Arguments to pass to the function. Must be a list.} \item{\code{package}}{Whether to keep the environment of \code{func} when passing it to the other package. Possible values are: \itemize{ \item \code{NULL} (the default): equivalent to \code{TRUE} if \code{func} inherits from \code{"crate"} (i.e. was created with \code{carrier::crate()}), and \code{FALSE} otherwise. \item \code{FALSE}: reset the environment to \code{.GlobalEnv}. \item \code{TRUE}: keep the environment as is. \item \code{pkg}: set the environment to the \code{pkg} package namespace. }} } \if{html}{\out{
}} } \subsection{Returns}{ The return value of the R expression. } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-run_with_output}{}}} \subsection{\code{r_session$run_with_output()}}{ Similar to \verb{$run()}, but returns the standard output and error of the child process as well. It does not throw on errors, but returns a non-\code{NULL} \code{error} member in the result list. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$run_with_output(func, args = list(), package = NULL)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{func}}{Function object to call in the background R process. Please read the notes for the similar argument of \code{\link[=r]{r()}}.} \item{\code{args}}{Arguments to pass to the function. Must be a list.} \item{\code{package}}{Whether to keep the environment of \code{func} when passing it to the other package. Possible values are: \itemize{ \item \code{NULL} (the default): equivalent to \code{TRUE} if \code{func} inherits from \code{"crate"} (i.e. was created with \code{carrier::crate()}), and \code{FALSE} otherwise. \item \code{FALSE}: reset the environment to \code{.GlobalEnv}. \item \code{TRUE}: keep the environment as is. \item \code{pkg}: set the environment to the \code{pkg} package namespace. }} } \if{html}{\out{
}} } \subsection{Returns}{ A list with the following entries. \itemize{ \item \code{result}: The value returned by \code{func}. On error this is \code{NULL}. \item \code{stdout}: The standard output of the process while evaluating \item \code{stderr}: The standard error of the process while evaluating the \code{func} call. \item \code{error}: On error it contains an error object, that contains the error thrown in the subprocess. Otherwise it is \code{NULL}. \item \code{code}, \code{message}: These fields are used by call internally and you can ignore them. } } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-call}{}}} \subsection{\code{r_session$call()}}{ Starts running a function in the background R session, and returns immediately. To check if the function is done, call the \code{poll_process()} method. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$call(func, args = list(), package = NULL)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{func}}{Function object to call in the background R process. Please read the notes for the similar argument of \code{\link[=r]{r()}}.} \item{\code{args}}{Arguments to pass to the function. Must be a list.} \item{\code{package}}{Whether to keep the environment of \code{func} when passing it to the other package. Possible values are: \itemize{ \item \code{NULL} (the default): equivalent to \code{TRUE} if \code{func} inherits from \code{"crate"} (i.e. was created with \code{carrier::crate()}), and \code{FALSE} otherwise. \item \code{FALSE}: reset the environment to \code{.GlobalEnv}. \item \code{TRUE}: keep the environment as is. \item \code{pkg}: set the environment to the \code{pkg} package namespace. }} } \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-poll_process}{}}} \subsection{\code{r_session$poll_process()}}{ Poll the R session with a timeout. If the session has finished the computation, it returns with \code{"ready"}. If the timeout is reached, it returns with \code{"timeout"}. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$poll_process(timeout)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{timeout}}{Timeout period in milliseconds.} } \if{html}{\out{
}} } \subsection{Returns}{ Character string \code{"ready"} or \code{"timeout"}. } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-get_state}{}}} \subsection{\code{r_session$get_state()}}{ Return the state of the R session. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$get_state()} \if{html}{\out{
}} } \subsection{Returns}{ Possible values: \itemize{ \item \code{"starting"}: starting up, \item \code{"idle"}: ready to compute, \item \code{"busy"}: computing right now, \item \code{"finished"}: the R process has finished. } } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-get_running_time}{}}} \subsection{\code{r_session$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 \code{NA} if there is no active computation. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$get_running_time()} \if{html}{\out{
}} } \subsection{Returns}{ Named vector of \code{POSIXct} objects. The names are \code{"total"} and \code{"current"}. } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-read}{}}} \subsection{\code{r_session$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. This is a low level function that you only need to use if you want to process events (messages) from the R session manually. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$read()} \if{html}{\out{
}} } \subsection{Returns}{ \code{NULL} if no events are available. Otherwise a named list, which is also a \code{callr_session_result} object. The list always has a \code{code} entry which is the type of the event. See also \code{r_session$public_fields$status} for symbolic names of the event types. \itemize{ \item \code{200}: (\code{DONE}) The computation is done, and the event includes the result, in the same form as for the \code{run()} method. \item \code{201}: (\code{STARTED}) An R session that was in 'starting' state is ready to go. \item \code{202}: (\code{ATTACH_DONE}) Used by the \code{attach()} method. \item \code{301}: (\code{MSG}) A message from the subprocess. The message is a condition object with class \code{callr_message}. (It typically has other classes, e.g. \code{cli_message} for output from the cli package.) \item \code{500}: (\code{EXITED}) The R session finished cleanly. This means that the evaluated expression quit R. \item \code{501}: (\code{CRASHED}) The R session crashed or was killed. \item \code{502}: (\code{CLOSED}) The R session closed its end of the connection that callr uses for communication. } } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-close}{}}} \subsection{\code{r_session$close()}}{ Terminate the current computation and the R process. The session object will be in \code{"finished"} state after this. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$close(grace = 1000)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{grace}}{Grace period in milliseconds, to wait for the subprocess to exit cleanly, after its standard input is closed. If the process is still running after this period, it will be killed.} } \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-traceback}{}}} \subsection{\code{r_session$traceback()}}{ The \code{traceback()} method can be used after an error in the R subprocess. It is equivalent to the \code{\link[base:traceback]{base::traceback()}} call, in the subprocess. On callr version 3.8.0 and above, you need to set the \code{callr.traceback} option to \code{TRUE} (in the main process) to make the subprocess save the trace on error. This is because saving the trace can be costly for large objects passed as arguments. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$traceback()} \if{html}{\out{
}} } \subsection{Returns}{ The same output as from \code{\link[base:traceback]{base::traceback()}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-debug}{}}} \subsection{\code{r_session$debug()}}{ Interactive debugger to inspect the dumped frames in the subprocess, after an error. See more at \link{r_session_debug}. On callr version 3.8.0 and above, you need to set the \code{callr.traceback} option to \code{TRUE} (in the main process) to make the subprocess dump frames on error. This is because saving the frames can be costly for large objects passed as arguments. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$debug()} \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-attach}{}}} \subsection{\code{r_session$attach()}}{ Experimental function that provides a REPL (Read-Eval-Print-Loop) to the subprocess. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$attach()} \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-print}{}}} \subsection{\code{r_session$print()}}{ Print method for an \code{r_session}. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$print(...)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{...}}{Arguments are not used currently.} } \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-cleanup}{}}} \subsection{\code{r_session$cleanup()}}{ Delete the temporary files created for this R session. Only call this if you are sure that the process is done and you don't need to read from it anymore. If you don't call this method explicitly, the temporary files will be deleted when the process object is garbage collected. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$cleanup()} \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-finalize}{}}} \subsection{\code{r_session$finalize()}}{ Finalizer that is called when garbage collecting an \code{r_session} object, to clean up temporary files. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$finalize()} \if{html}{\out{
}} } } \if{html}{\out{

}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-r_session-clone}{}}} \subsection{\code{r_session$clone()}}{ The objects of this class are cloneable with this method. \subsection{Usage}{ \if{html}{\out{
}} \preformatted{r_session$clone(deep = FALSE)} \if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ \item{\code{deep}}{Whether to make a deep clone.} } \if{html}{\out{
}} } } }