例如,此 PowerShell 命令返回目录中前 5 个最大的文件:
gci -r |sort Length -desc |select fullname -f 5
是否可以在 R 中运行它并将其分配给变量?
我试过这个:
system("gci -r|sort Length -desc|select fullname -f 5")
Warning message:
running command 'gci -r|sort Length -desc|select fullname -f 5' had status 127
我不应该使用system()
here?
您可能需要运行它(假设 PowerShell 在您的路径中):
system("powershell -command \"gci -r|sort Length -desc|select fullname -f 5\"")
或者,如果你不想逃跑"
with \"
.
system('powershell -command "gci -r|sort Length -desc|select fullname -f 5"')
我还假设这就是 R 在字符串中转义和嵌入引号的方式(来自我粗略地谷歌搜索 R 中的字符串处理)。
如果您希望将输出捕获到变量(特别是字符向量),您需要使用intern = TRUE
争论:
res <- system('powershell -command "gci -r|sort Length -desc|select fullname -f 5"', intern=TRUE)
欲了解更多信息,请参阅:
http://stat.ethz.ch/R-manual/R-patched/library/base/html/system.html http://stat.ethz.ch/R-manual/R-patched/library/base/html/system.html
尤其:
If intern = TRUE
,给出命令输出的字符向量,每个字符串一行。
and
If intern = FALSE
,返回值是错误代码(0表示成功),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)