简体   繁体   中英

Pass output from system2 into R object

I'm trying to get the output from a binary with args called by system2 into an R object. But, I fail. I've googled for several alternatives such as system, exec_internal but cannot get it right. Here is a toy example using Linux' function "factor" which should factorize 5555.

test_001 <- system2("factor", args=c("5555"))

and the output shown on the monitor is

5555: 5 11 101

However, I would like to have that result into the object "test_001". But if I type

test_001

the result is only

[1] 0

I really don't understand how to get the output from system2 into an R object. Thanks for any help!

You have to change the location where the output is sent to by specifying stdout (and possibly etderr ) option. As the help of system2 indicates the default output is R console

where output to 'stdout' or 'stderr' should be sent. Possible values are "", to the R console (the default), NULL or FALSE (discard output), TRUE (capture the output in a character vector) or a character string naming a file.

output = TRUE send the output as a character vector object in R. So for your case you can:

test_001 <- system2("factor", args=c("5555"), stdout = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM