Pass output from system2 into R object

870 Views Asked by At

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!

1

There are 1 best solutions below

0
amatsuo_net On

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)