【问题标题】:Running Windows Command Prompt using R使用 R 运行 Windows 命令提示符
【发布时间】:2015-12-24 07:52:20
【问题描述】:

我对 Windows 命令提示符比较陌生。请问是否可以使用 R 运行 windows 命令提示符?

也就是说, 我想使用 R 将命令输入到 Windows 命令提示符中。

提前感谢您!

【问题讨论】:

    标签: windows rstudio command-prompt


    【解决方案1】:

    在 R 中,您可以使用 system2 执行任何系统命令。 Windows 命令提示符的命令是cmd。然后,您可以使用system2args 参数传递参数。

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

    因此,您的命令应该如下所示:

    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。为了使整个内容更具可读性,您可以这样使用它:

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

    【讨论】:

    • system2("cmd.exe", args = "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") 错误:意外';'在 "system2("cmd.exe", args = "java -mx150m -cp ";" > system2("cmd.exe", args = "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")
    • 我尝试运行此命令,该命令在我的命令提示符下运行良好,但出现此错误。你知道为什么会出现这样的错误吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2010-11-30
    相关资源
    最近更新 更多