Running Windows Command Prompt using R

a 夏天 提交于 2019-12-12 01:41:44

问题


I am rather new to Windows Command Prompt. May I know if it is possible to run windows command Prompt using R?

Ie, I would like to use R to input a command into windows command prompt.

Thank you in advance!


回答1:


In R you can execute any system command using system2. The command for the windows command prompt is cmd. You could then pass arguments using the args parameter of system2.

If your arguments include quotes " you need to escape them using \, e.g. system2("cmd", args = c("/c", "echo", "hello \"world\"")), which executes cmd passing /c as first argument which lets cmd execute echo passing hello "world" as argument.

Your command should thus probably look like:

system2("cmd.exe", args = "/c java -mx150m -cp \";\" edu.stanford.nlp.parser.lexparser.LexicalizedParser -outputFormat \"penn,typedDependencies\" -outputFormatOptions \"basicDependencies\" edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ./Test/input.txt")

Note that you can invoke java directly. To make the whole thing a bit more readable, you could use it like this:

mx <- "-mx150m"
cp <- "-cp \";\" edu.stanford.nlp.parser.lexparser.LexicalizedParser"
of <- "-outputFormat \"penn,typedDependencies\""
oo <- "-outputFormatOptions \"basicDependencies\""
i <- "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"
o <- "./Test/input.txt"

system2("java", args = c(mx, cp, of, oo, i, o))


来源:https://stackoverflow.com/questions/34449241/running-windows-command-prompt-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!