使用R运行Windows命令提示符 [英] Running Windows Command Prompt using R

查看:184
本文介绍了使用R运行Windows命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Windows命令提示符不熟悉。我可以知道是否可以使用R运行Windows命令提示符吗?

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


我想使用R在Windows命令提示符下输入命令。

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

谢谢!

推荐答案

在R中,您可以使用 system2 。 Windows命令提示符下的命令为 cmd 。然后,您可以使用 system2 args 参数传递参数。

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.

如果您的参数包含引号 ,则需要使用 \ 将其转义,例如 system2( cmd,args = c( / c, echo, hello \ world\)),执行 cmd 传递 / c 作为第一个参数,它使 cmd 执行 echo 传递 hello world 作为参数。

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")

请注意,您可以调用 java 。为了使整个内容更具可读性,您可以像这样使用它:

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))

这篇关于使用R运行Windows命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆