2

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Nice to hear that
This works for this command but what I am trying to really to is use res <- system("impala-shell -i....",intern=TRUE) and when there is a query error, capture the error. Adding 2>&1 doesn't seem to do it here
This method is not working for other errors like if the command does not exist. Any other workaround?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.