我正在尝试找出如何从system()
捕获错误消息
例如:
> 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:无法访问home:没有这样的文件或目录”?
最佳答案
这样尝试,即重定向 stderr 数据
res <- system("ls home 2>&1",intern=TRUE)
这将导致
[1] "ls: home: No such file or directory"
attr(,"status")
[1] 1
关于R 使用 system() 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666722/