我想在代码中使用R的System函数,因此我编写了简单的单行代码来检查其是否正常运行。这是我的一支类轮-
system("dir", intern = TRUE)
我正在使用Windows,因此它应该给我与命令提示符相同的输出(显示当前目录中存在的所有文件)。而是导致了错误:
Error in system("dir", intern = TRUE) : 'dir' not found
最佳答案
根据?system
的描述:
command must be an executable (extensions ‘.exe’, ‘.com’) or a batch file (extensions ‘.cmd’ and ‘.bat’): these extensions are tried in turn if none is supplied. This means that redirection, pipes, DOS internal commands, ... cannot be used: see shell if you want to pass a shell command-line.
因此,不能使用DOS内部命令。它实际上不会为我返回任何错误,但它不会返回任何内容。
但是,
shell
可以按预期工作: shell('dir', intern = TRUE)
#[1] " Volume in drive C is "
#[2] " Volume Serial Number is "
#[3] ""
#[4] " Directory of C:\\Users\\TB\\Documents"
#[5] ""
#[6] "07/06/2019 11:10 <DIR> ."
#.....
关于r - 错误: 'dir' command not found while using System function in R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56629886/