user295944
user295944

Reputation: 373

R return error using system()

I am trying to find out how to capture the error message from system()

EX:

> res <- system("ls home",intern=TRUE)
ls: cannot access home: No such file or directory
Warning message:
running command 'ls home' had status 2 
> res
character(0)
attr(,"status")
[1] 2

Is there a way to capture the "ls: cannot access home: No such file or directory" in res?

Upvotes: 2

Views: 1803

Answers (1)

DAXaholic
DAXaholic

Reputation: 35428

Try it like that, i.e. redirecting the stderr data

res <- system("ls home 2>&1",intern=TRUE)

which will result in

[1] "ls: home: No such file or directory"
attr(,"status")
[1] 1

Upvotes: 3

Related Questions