R使用system()返回错误

时间:2016-09-23 17:48:06

标签: r

我正在尝试了解如何从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

有没有办法在res中捕获“ls:无法访问主页:没有这样的文件或目录”?

1 个答案:

答案 0 :(得分:2)

尝试这样,即重定向stderr数据

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

将导致

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