[英]How to run PowerShell command in R?
例如,此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()
嗎?
你可能需要運行它(假設PowerShell在你的路徑中):
system("powershell -command \"gci -r|sort Length -desc|select fullname -f 5\"")
或者,如果你不熱衷於逃避"
與\\"
。
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
特別是:
如果
intern = TRUE
,則給出命令輸出的字符向量,每個字符串一行。
和
如果
intern = FALSE
,則返回值為錯誤代碼(成功為0),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.