问题
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