我经常使用Windows命令tree
来打印出文件内容的树状图。我想将其添加到R中的工作流程中,但是无法使命令运行,system
或者shell.exec
无法弄清楚原因。这是一个可重现的示例,其中包含4种方法和shQuote
在system
andshell.exec
命令中使用(有和没有)的错误消息。当我使用时cat
,可以将命令粘贴到Windows命令行手册中(不使用R),并生成树形图。我无法在R中运行该程序,我做错了什么?
## build some mock files and directories with lapply
lapply(file.path("TEMP", c("", "X", "Y")), dir.create)
lapply(file.path("TEMP", paste0(c("A", "B"), ".txt")), file.create)
## create path to external file
out <- file.path(getwd(), "TREE.txt")
## create the tree command
cmd <- paste("tree", shQuote(file.path(getwd(), "TEMP")), "/f /a >", shQuote(out))
cat(cmd) ## view it
system(cmd) ## attempt 1
cmd <- paste("tree", shQuote(file.path(getwd(), "TEMP")), "/f /a >", shQuote(out))
shell.exec(cmd) ## attempt 2
cmd2 <- paste("tree", file.path(getwd(), "TEMP"), "/f /a >", out)
system(cmd2) ## attempt 3
shell.exec(cmd2) ## attempt 4
cat(cmd, file="clipboard")
## > out <- file.path(getwd(), "TREE.txt")
## > cmd <- paste("tree", shQuote(file.path(getwd(), "TEMP")), "/f /a >", shQuote(out))
## > cat(cmd)
## tree "C:/Users/trinker/Desktop/TEMP" /f /a > "C:/Users/trinker/Desktop/TREE.txt">
## > system(cmd)
## Too many parameters - >
## >
## > cmd <- paste("tree", shQuote(file.path(getwd(), "TEMP")), "/f /a >", shQuote(out))
## > shell.exec(cmd)
## Error in shell.exec(cmd) :
## 'tree "C:/Users/trinker/Desktop/TEMP" /f /a > "C:/Users/trinker/Desktop/TREE.txt"' not found
## >
## >
## > cmd2 <- paste("tree", file.path(getwd(), "TEMP"), "/f /a >", out)
## > system(cmd2)
## Too many parameters - >
## > shell.exec(cmd2)
## Error in shell.exec(cmd2) :
## 'tree C:/Users/trinker/Desktop/TEMP /f /a > C:/Users/trinker/Desktop/TREE.txt' not found
使用shell
。在Windows上,system
不使用外壳程序(它仅运行系统命令)。您正在尝试使用管道重定向输出,因此遇到了以下问题? system
:
command
必须是可执行文件(扩展名“ .exe”,“。com”)或批处理文件(扩展名“ .cmd”和“ .bat”):如果未提供扩展名,则依次尝试使用这些扩展名。)这表示重定向,管道,DOS内部命令,...无法使用:查看shell
是否要传递shell命令行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句