David Z
David Z

Reputation: 7041

How to capture system2() output in R

Here is an example:

RR <- "/usr/bin/R"
x <- system2(RR, "--version")
R version 3.3.3 (2017-03-06) -- "Another Canoe"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
http://www.gnu.org/licenses/.
> x
[1] 0

As you can see here the x is 0. My question is how to assign the system2(RR, "--version") output to x.

Upvotes: 9

Views: 2181

Answers (2)

user890739
user890739

Reputation: 733

Using system instead:

x <- system(paste(RR,'--version'), intern=TRUE)

Upvotes: 0

MrFlick
MrFlick

Reputation: 206242

This is described in the documentation for ?system2. If you want to capture the output of your program as a character vector, set stdout=TRUE

x <- system2(RR, "--version", stdout=TRUE)

"stdout" is for the "standard output" of the program. It's a common term used by programs to identify the "result" from a program.

Upvotes: 8

Related Questions