Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit372bd33

Browse files
committed
knitr::convert_chunk_header(type = "yaml")
1 parent5716765 commit372bd33

File tree

3 files changed

+196
-125
lines changed

3 files changed

+196
-125
lines changed

‎README.Rmd‎

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ editor_options:
1111
wrap:sentence
1212
---
1313

14-
```{r, setup, include = FALSE}
14+
```{r}
15+
#| label: setup
16+
#| include: false
1517
knitr::opts_chunk$set(
1618
comment = "#>",
1719
fig.path = "man/figures",
@@ -44,13 +46,19 @@ asciicast::init_knitr_engine(
4446

4547
Install the stable version from CRAN:
4648

47-
```{r, asciicast-setup, eval = FALSE, cache = FALSE}
49+
```{r}
50+
#| label: asciicast-setup
51+
#| eval: false
52+
#| cache: false
4853
install.packages("callr")
4954
```
5055

5156
Install the development version from GitHub:
5257

53-
```{r, asciicast-setup2, eval = FALSE, cache = FALSE}
58+
```{r}
59+
#| label: asciicast-setup2
60+
#| eval: false
61+
#| cache: false
5462
pak::pak("r-lib/callr")
5563
```
5664

@@ -59,7 +67,8 @@ pak::pak("r-lib/callr")
5967
Use`r()` to run an R function in a new R process.
6068
The results are passed back seamlessly:
6169

62-
```{asciicast simple}
70+
```{asciicast}
71+
#| label: simple
6372
callr::r(function() var(iris[, 1:4]))
6473
```
6574

@@ -69,14 +78,16 @@ You can pass arguments to the function by setting `args` to the list of argument
6978
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.
7079
For example, the following does not work:
7180

72-
```{asciicast passargsfail}
81+
```{asciicast}
82+
#| label: passargsfail
7383
mycars <- cars
7484
callr::r(function() summary(mycars))
7585
```
7686

7787
But this does:
7888

79-
```{asciicast, passargsok}
89+
```{asciicast}
90+
#| label: passargsok
8091
mycars <- cars
8192
callr::r(function(x) summary(x), args = list(mycars))
8293
```
@@ -88,20 +99,23 @@ Note that the arguments will be serialized and saved to a file, so if they are l
8899
You can use any R package in the child process, just make sure to refer to it explicitly with the`::` operator.
89100
For example, the following code creates an[igraph](https://github.com/igraph/rigraph) graph in the child, and calculates some metrics of it.
90101

91-
```{asciicast packages}
102+
```{asciicast}
103+
#| label: packages
92104
callr::r(function() { g <- igraph::sample_gnp(1000, 4/1000); igraph::diameter(g) })
93105
```
94106

95107
###Error handling
96108

97109
callr copies errors from the child process back to the main R session:
98110

99-
```{asciicast error1}
111+
```{asciicast}
112+
#| label: error1
100113
callr::r(function() 1 + "A")
101114
```
102115
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.
103116

104-
```{asciicast error2-2}
117+
```{asciicast}
118+
#| label: error2-2
105119
.Last.error
106120
```
107121

@@ -116,14 +130,17 @@ The top part of the trace contains the frames in the main process, and the botto
116130

117131
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:
118132

119-
```{asciicast io, results = "hide"}
133+
```{asciicast}
134+
#| label: io
135+
#| results: hide
120136
x <- callr::r(function() { print("hello world!"); message("hello again!") },
121137
stdout = "/tmp/out", stderr = "/tmp/err"
122138
)
123139
readLines("/tmp/out")
124140
```
125141

126-
```{asciicast io-2}
142+
```{asciicast}
143+
#| label: io-2
127144
readLines("/tmp/err")
128145
```
129146

@@ -135,14 +152,16 @@ The `show = TRUE` options will also show the output of the child, as it is print
135152
`r_bg()` is similar to`r()` but it starts the R process in the background.
136153
It returns an`r_process` R6 object, that provides a rich API:
137154

138-
```{asciicast bg}
155+
```{asciicast}
156+
#| label: bg
139157
rp <- callr::r_bg(function() Sys.sleep(.2))
140158
rp
141159
```
142160

143161
This is a list of all`r_process` methods:
144162

145-
```{asciicast, bg-methods}
163+
```{asciicast}
164+
#| label: bg-methods
146165
ls(rp)
147166
```
148167

@@ -165,29 +184,34 @@ Multiple background R processes are best managed with the `processx::poll()` fun
165184
It returns as soon as one process has generated an event, or if its timeout has expired.
166185
The timeout is in milliseconds.
167186

168-
```{asciicast poll}
187+
```{asciicast}
188+
#| label: poll
169189
rp1 <- callr::r_bg(function() { Sys.sleep(1/2); "1 done" })
170190
rp2 <- callr::r_bg(function() { Sys.sleep(1/1000); "2 done" })
171191
processx::poll(list(rp1, rp2), 1000)
172192
```
173193

174-
```{asciicast poll-2}
194+
```{asciicast}
195+
#| label: poll-2
175196
rp2$get_result()
176197
```
177198

178-
```{asciicast poll-3}
199+
```{asciicast}
200+
#| label: poll-3
179201
processx::poll(list(rp1), 1000)
180202
```
181203

182-
```{asciicast poll-4}
204+
```{asciicast}
205+
#| label: poll-4
183206
rp1$get_result()
184207
```
185208

186209
##Persistent R sessions
187210

188211
`r_session` is another`processx::process` subclass that represents a persistent background R session:
189212

190-
```{asciicast rsession}
213+
```{asciicast}
214+
#| label: rsession
191215
rs <- callr::r_session$new()
192216
rs
193217
```
@@ -198,21 +222,25 @@ The `r_session$poll_process()` method or `processx::poll()` can then be used to
198222

199223
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.
200224

201-
```{asciicast rsession2}
225+
```{asciicast}
226+
#| label: rsession2
202227
rs <- callr::r_session$new()
203228
rs$run(function() runif(10))
204229
```
205230

206-
```{asciicast rsession2-2}
231+
```{asciicast}
232+
#| label: rsession2-2
207233
rs$call(function() rnorm(10))
208234
rs
209235
```
210236

211-
```{asciicast rsession-4}
237+
```{asciicast}
238+
#| label: rsession-4
212239
rs$poll_process(2000)
213240
```
214241

215-
```{asciicast rsession-5}
242+
```{asciicast}
243+
#| label: rsession-5
216244
rs$read()
217245
```
218246

@@ -221,7 +249,8 @@ rs$read()
221249
The`rcmd()` function calls an`R CMD` command.
222250
For example, you can call`R CMD INSTALL`,`R CMD check` or`R CMD config` this way:
223251

224-
```{asciicast rcmd}
252+
```{asciicast}
253+
#| label: rcmd
225254
callr::rcmd("config", "CC")
226255
```
227256

‎README.md‎

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,38 @@ that.
2121

2222
---
2323

24-
-[Features](#features)
25-
-[Installation](#installation)
26-
-[Synchronous, one-off R processes](#synchronous-one-off-r-processes)
27-
-[Passing arguments](#passing-arguments)
28-
-[Using packages](#using-packages)
29-
-[Error handling](#error-handling)
30-
-[Standard output and error](#standard-output-and-error)
31-
-[Background R processes](#background-r-processes)
32-
-[Multiple background R processes and
33-
`poll()`](#multiple-background-r-processes-and-poll)
34-
-[Persistent R sessions](#persistent-r-sessions)
35-
-[Running`R CMD` commands](#running-r-cmd-commands)
36-
-[Code of Conduct](#code-of-conduct)
24+
-[Features](#features)
25+
-[Installation](#installation)
26+
-[Synchronous, one-off R processes](#synchronous-one-off-r-processes)
27+
-[Passing arguments](#passing-arguments)
28+
-[Using packages](#using-packages)
29+
-[Error handling](#error-handling)
30+
-[Standard output and error](#standard-output-and-error)
31+
-[Background R processes](#background-r-processes)
32+
-[Multiple background R processes and
33+
`poll()`](#multiple-background-r-processes-and-poll)
34+
-[Persistent R sessions](#persistent-r-sessions)
35+
-[Running`R CMD` commands](#running-r-cmd-commands)
36+
-[Configuration](#configuration)
37+
-[Environment variables](#environment-variables)
38+
-[Code of Conduct](#code-of-conduct)
3739

3840
##Features
3941

40-
- Calls an R function, with arguments, in a subprocess.
41-
- Copies function arguments to the subprocess and copies the return
42-
value of the function back, seamlessly.
43-
- Copies error objects back from the subprocess, including a stack
44-
trace.
45-
- Shows and/or collects the standard output and standard error of the
46-
subprocess.
47-
- Supports both one-off and persistent R subprocesses.
48-
- Calls the function synchronously or asynchronously (in the
49-
background).
50-
- Can call`R CMD` commands, synchronously or asynchronously.
51-
- Can call R scripts, synchronously or asynchronously.
52-
- Provides extensible`r_process`,`rcmd_process` and`rscript_process`
53-
R6 classes, based on`processx::process`.
42+
-Calls an R function, with arguments, in a subprocess.
43+
-Copies function arguments to the subprocess and copies the return
44+
value of the function back, seamlessly.
45+
-Copies error objects back from the subprocess, including a stack
46+
trace.
47+
-Shows and/or collects the standard output and standard error of the
48+
subprocess.
49+
-Supports both one-off and persistent R subprocesses.
50+
-Calls the function synchronously or asynchronously (in the
51+
background).
52+
-Can call`R CMD` commands, synchronously or asynchronously.
53+
-Can call R scripts, synchronously or asynchronously.
54+
-Provides extensible`r_process`,`rcmd_process` and
55+
`rscript_process`R6 classes, based on`processx::process`.
5456

5557
##Installation
5658

@@ -217,17 +219,18 @@ These include all methods of the `processx::process` superclass and the
217219
new`get_result()` method, to retrieve the R object returned by the
218220
function call. Some of the handiest methods are:
219221

220-
-`get_exit_status()` to query the exit status of a finished process.
221-
-`get_result()` to collect the return value of the R function call.
222-
-`interrupt()` to send an interrupt to the process. This is equivalent
223-
to a`CTRL+C` key press, and the R process might ignore it.
224-
-`is_alive()` to check if the process is alive.
225-
-`kill()` to terminate the process.
226-
-`poll_io()` to wait for any standard output, standard error, or the
227-
completion of the process, with a timeout.
228-
-`read_*()` to read the standard output or error.
229-
-`suspend()` and`resume()` to stop and continue a process.
230-
-`wait()` to wait for the completion of the process, with a timeout.
222+
-`get_exit_status()` to query the exit status of a finished process.
223+
-`get_result()` to collect the return value of the R function call.
224+
-`interrupt()` to send an interrupt to the process. This is
225+
equivalent to a`CTRL+C` key press, and the R process might ignore
226+
it.
227+
-`is_alive()` to check if the process is alive.
228+
-`kill()` to terminate the process.
229+
-`poll_io()` to wait for any standard output, standard error, or the
230+
completion of the process, with a timeout.
231+
-`read_*()` to read the standard output or error.
232+
-`suspend()` and`resume()` to stop and continue a process.
233+
-`wait()` to wait for the completion of the process, with a timeout.
231234

232235
##Multiple background R processes and`poll()`
233236

@@ -350,14 +353,14 @@ standard error, and the exit (status) code of the `R CMD` command.
350353

351354
###Environment variables
352355

353-
*`CALLR_NO_TEMP_DLLS`: If`true`, then callr does not use a temporary
354-
directory to copy the client DLL files from, in the subprocess. By
355-
default callr copies the DLL file that drives the callr subprocess into
356-
a temporary directory and loads it from there. This is mainly to avoid
357-
locking a DLL file in the package library, on Windows. If this default
358-
causes issues for you, set it to`true`, and then callr will use the DLL
359-
file from the installed processx package. See also
360-
https://github.com/r-lib/callr/issues/273.
356+
-`CALLR_NO_TEMP_DLLS`: If`true`, then callr does not use a temporary
357+
directory to copy the client DLL files from, in the subprocess. By
358+
default callr copies the DLL file that drives the callr subprocess
359+
intoa temporary directory and loads it from there. This is mainly
360+
to avoidlocking a DLL file in the package library, on Windows. If
361+
this defaultcauses issues for you, set it to`true`, and then callr
362+
will use the DLLfile from the installed processx package. See also
363+
<https://github.com/r-lib/callr/issues/273>.
361364

362365
##Code of Conduct
363366

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp