-
Notifications
You must be signed in to change notification settings - Fork 38
/
README.Rmd
247 lines (184 loc) · 7.98 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
---
output:
github_document:
toc: true
toc_depth: 3
includes:
before_body: inst/header.md
always_allow_html: yes
editor_options:
markdown:
wrap: sentence
---
```{r, setup, include = FALSE}
knitr::opts_chunk$set(
comment = "#>",
fig.path = "man/figures",
fig.width = 10,
asciicast_theme = if (Sys.getenv("IN_PKGDOWN") == "true") "pkgdown" else "readme"
)
asciicast::init_knitr_engine(
echo = TRUE,
echo_input = FALSE,
startup = quote({
library(cli)
options(cli.num_colors = cli::truecolor)
})
)
```
## Features
- Calls an R function, with arguments, in a subprocess.
- Copies function arguments to the subprocess and copies the return value of the function back, seamlessly.
- Copies error objects back from the subprocess, including a stack trace.
- Shows and/or collects the standard output and standard error of the subprocess.
- Supports both one-off and persistent R subprocesses.
- Calls the function synchronously or asynchronously (in the background).
- Can call `R CMD` commands, synchronously or asynchronously.
- Can call R scripts, synchronously or asynchronously.
- Provides extensible `r_process`, `rcmd_process` and `rscript_process` R6 classes, based on `processx::process`.
## Installation
Install the stable version from CRAN:
```{r, asciicast-setup, eval = FALSE, cache = FALSE}
install.packages("callr")
```
Install the development version from GitHub:
```{r, asciicast-setup2, eval = FALSE, cache = FALSE}
pak::pak("r-lib/callr")
```
## Synchronous, one-off R processes
Use `r()` to run an R function in a new R process.
The results are passed back seamlessly:
```{asciicast simple}
callr::r(function() var(iris[, 1:4]))
```
### Passing arguments
You can pass arguments to the function by setting `args` to the list of arguments.
This is often necessary as these arguments are explicitly copied to the child process, whereas the evaluated function cannot refer to variables in the parent.
For example, the following does not work:
```{asciicast passargsfail}
mycars <- cars
callr::r(function() summary(mycars))
```
But this does:
```{asciicast, passargsok}
mycars <- cars
callr::r(function(x) summary(x), args = list(mycars))
```
Note that the arguments will be serialized and saved to a file, so if they are large R objects, it might take a long time for the child process to start up.
### Using packages
You can use any R package in the child process, just make sure to refer to it explicitly with the `::` operator.
For example, the following code creates an [igraph](https://github.com/igraph/rigraph) graph in the child, and calculates some metrics of it.
```{asciicast packages}
callr::r(function() { g <- igraph::sample_gnp(1000, 4/1000); igraph::diameter(g) })
```
### Error handling
callr copies errors from the child process back to the main R session:
```{asciicast error1}
callr::r(function() 1 + "A")
```
callr sets the `.Last.error` variable, and after an error you can inspect this for more details about the error, including stack traces both from the main R process and the subprocess.
```{asciicast error2-2}
.Last.error
```
The error objects has two parts.
The first belongs to the main process, and the second belongs to the subprocess.
`.Last.error` also includes a stack trace, that includes both the main R process and the subprocess:
The top part of the trace contains the frames in the main process, and the bottom part contains the frames in the subprocess, starting with the anonymous function.
### Standard output and error
By default, the standard output and error of the child is lost, but you can request callr to redirect them to files, and then inspect the files in the parent:
```{asciicast io, results = "hide"}
x <- callr::r(function() { print("hello world!"); message("hello again!") },
stdout = "/tmp/out", stderr = "/tmp/err"
)
readLines("/tmp/out")
```
```{asciicast io-2}
readLines("/tmp/err")
```
With the `stdout` option, the standard output is collected and can be examined once the child process finished.
The `show = TRUE` options will also show the output of the child, as it is printed, on the console of the parent.
## Background R processes
`r_bg()` is similar to `r()` but it starts the R process in the background.
It returns an `r_process` R6 object, that provides a rich API:
```{asciicast bg}
rp <- callr::r_bg(function() Sys.sleep(.2))
rp
```
This is a list of all `r_process` methods:
```{asciicast, bg-methods}
ls(rp)
```
These include all methods of the `processx::process` superclass and the new `get_result()` method, to retrieve the R object returned by the function call.
Some of the handiest methods are:
- `get_exit_status()` to query the exit status of a finished process.
- `get_result()` to collect the return value of the R function call.
- `interrupt()` to send an interrupt to the process. This is equivalent to a `CTRL+C` key press, and the R process might ignore it.
- `is_alive()` to check if the process is alive.
- `kill()` to terminate the process.
- `poll_io()` to wait for any standard output, standard error, or the completion of the process, with a timeout.
- `read_*()` to read the standard output or error.
- `suspend()` and `resume()` to stop and continue a process.
- `wait()` to wait for the completion of the process, with a timeout.
## Multiple background R processes and `poll()`
Multiple background R processes are best managed with the `processx::poll()` function that waits for events (standard output/error or termination) from multiple processes.
It returns as soon as one process has generated an event, or if its timeout has expired.
The timeout is in milliseconds.
```{asciicast poll}
rp1 <- callr::r_bg(function() { Sys.sleep(1/2); "1 done" })
rp2 <- callr::r_bg(function() { Sys.sleep(1/1000); "2 done" })
processx::poll(list(rp1, rp2), 1000)
```
```{asciicast poll-2}
rp2$get_result()
```
```{asciicast poll-3}
processx::poll(list(rp1), 1000)
```
```{asciicast poll-4}
rp1$get_result()
```
## Persistent R sessions
`r_session` is another `processx::process` subclass that represents a persistent background R session:
```{asciicast rsession}
rs <- callr::r_session$new()
rs
```
`r_session$run()` is a synchronous call, that works similarly to `r()`, but uses the persistent session.
`r_session$call()` starts the function call and returns immediately.
The `r_session$poll_process()` method or `processx::poll()` can then be used to wait for the completion or other events from one or more R sessions, R processes or other `processx::process` objects.
Once an R session is done with an asynchronous computation, its `poll_process()` method returns `"ready"` and the `r_session$read()` method can read out the result.
```{asciicast rsession2}
rs <- callr::r_session$new()
rs$run(function() runif(10))
```
```{asciicast rsession2-2}
rs$call(function() rnorm(10))
rs
```
```{asciicast rsession-4}
rs$poll_process(2000)
```
```{asciicast rsession-5}
rs$read()
```
## Running `R CMD` commands
The `rcmd()` function calls an `R CMD` command.
For example, you can call `R CMD INSTALL`, `R CMD check` or `R CMD config` this way:
```{asciicast rcmd}
callr::rcmd("config", "CC")
```
This returns a list with three components: the standard output, the standard error, and the exit (status) code of the `R CMD` command.
## Configuration
### Environment variables
* `CALLR_NO_TEMP_DLLS`: If `true`, then callr does not use a temporary
directory to copy the client DLL files from, in the subprocess. By
default callr copies the DLL file that drives the callr subprocess into
a temporary directory and loads it from there. This is mainly to avoid
locking a DLL file in the package library, on Windows. If this default
causes issues for you, set it to `true`, and then callr will use the DLL
file from the installed processx package. See also
https://github.com/r-lib/callr/issues/273.
## Code of Conduct
Please note that the callr project is released with a
[Contributor Code of Conduct](https://callr.r-lib.org/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.