You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
@@ -69,14 +78,16 @@ You can pass arguments to the function by setting `args` to the list of argument
69
78
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.
@@ -88,20 +99,23 @@ Note that the arguments will be serialized and saved to a file, so if they are l
88
99
You can use any R package in the child process, just make sure to refer to it explicitly with the`::` operator.
89
100
For example, the following code creates an[igraph](https://github.com/igraph/rigraph) graph in the child, and calculates some metrics of it.
90
101
91
-
```{asciicast packages}
102
+
```{asciicast}
103
+
#| label: packages
92
104
callr::r(function() { g <- igraph::sample_gnp(1000, 4/1000); igraph::diameter(g) })
93
105
```
94
106
95
107
###Error handling
96
108
97
109
callr copies errors from the child process back to the main R session:
98
110
99
-
```{asciicast error1}
111
+
```{asciicast}
112
+
#| label: error1
100
113
callr::r(function() 1 + "A")
101
114
```
102
115
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.
103
116
104
-
```{asciicast error2-2}
117
+
```{asciicast}
118
+
#| label: error2-2
105
119
.Last.error
106
120
```
107
121
@@ -116,14 +130,17 @@ The top part of the trace contains the frames in the main process, and the botto
116
130
117
131
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:
118
132
119
-
```{asciicast io, results = "hide"}
133
+
```{asciicast}
134
+
#| label: io
135
+
#| results: hide
120
136
x <- callr::r(function() { print("hello world!"); message("hello again!") },
121
137
stdout = "/tmp/out", stderr = "/tmp/err"
122
138
)
123
139
readLines("/tmp/out")
124
140
```
125
141
126
-
```{asciicast io-2}
142
+
```{asciicast}
143
+
#| label: io-2
127
144
readLines("/tmp/err")
128
145
```
129
146
@@ -135,14 +152,16 @@ The `show = TRUE` options will also show the output of the child, as it is print
135
152
`r_bg()` is similar to`r()` but it starts the R process in the background.
136
153
It returns an`r_process` R6 object, that provides a rich API:
137
154
138
-
```{asciicast bg}
155
+
```{asciicast}
156
+
#| label: bg
139
157
rp <- callr::r_bg(function() Sys.sleep(.2))
140
158
rp
141
159
```
142
160
143
161
This is a list of all`r_process` methods:
144
162
145
-
```{asciicast, bg-methods}
163
+
```{asciicast}
164
+
#| label: bg-methods
146
165
ls(rp)
147
166
```
148
167
@@ -165,29 +184,34 @@ Multiple background R processes are best managed with the `processx::poll()` fun
165
184
It returns as soon as one process has generated an event, or if its timeout has expired.
`r_session` is another`processx::process` subclass that represents a persistent background R session:
189
212
190
-
```{asciicast rsession}
213
+
```{asciicast}
214
+
#| label: rsession
191
215
rs <- callr::r_session$new()
192
216
rs
193
217
```
@@ -198,21 +222,25 @@ The `r_session$poll_process()` method or `processx::poll()` can then be used to
198
222
199
223
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.
200
224
201
-
```{asciicast rsession2}
225
+
```{asciicast}
226
+
#| label: rsession2
202
227
rs <- callr::r_session$new()
203
228
rs$run(function() runif(10))
204
229
```
205
230
206
-
```{asciicast rsession2-2}
231
+
```{asciicast}
232
+
#| label: rsession2-2
207
233
rs$call(function() rnorm(10))
208
234
rs
209
235
```
210
236
211
-
```{asciicast rsession-4}
237
+
```{asciicast}
238
+
#| label: rsession-4
212
239
rs$poll_process(2000)
213
240
```
214
241
215
-
```{asciicast rsession-5}
242
+
```{asciicast}
243
+
#| label: rsession-5
216
244
rs$read()
217
245
```
218
246
@@ -221,7 +249,8 @@ rs$read()
221
249
The`rcmd()` function calls an`R CMD` command.
222
250
For example, you can call`R CMD INSTALL`,`R CMD check` or`R CMD config` this way: