【问题标题】:Setting Directory of command prompt设置命令提示符目录
【发布时间】:2015-12-28 02:02:47
【问题描述】:

我想在 R 中使用函数system2 设置命令提示符的工作目录,但出现以下错误:

system2("cmd.exe", args = paste("cd", "Users/AKatherine/Downloads", sep = " "))

'Akatherine' 未被识别为内部或外部命令, 可运行的程序或批处理文件。

另外,我尝试运行这个

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 "*;"

有人知道为什么吗?谁能帮我解决这个问题?

【问题讨论】:

  • 我相当肯定您在转义字符方面存在问题。但也可能存在其他问题。
  • 问题:命令cd Users/AKatherine/Downloads 对打开命令提示符的目录有意义吗?

标签: r cmd command-prompt


【解决方案1】:

对于我的问题的第二部分,我尝试使用 shell 函数而不是 system2 函数,当我在命令提示符下键入相同的命令时,我得到了相同的输出。我用过:

shell(cmd = "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", shell = "cmd.exe") 

而不是

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() 在 Windows 上不会像您期望的那样工作。 shell() 是去这里的路。
  • 请注意,system2 完全可用,而且是更好的选择,请参阅我对原始问题的更新答案:stackoverflow.com/questions/34449241/…
  • @CaringDev 你介意解释一下为什么 system2 比 shell 更好吗?
  • @Nana "system2 是 R 2.12.0 中引入的比 system 更便携和灵活的接口。它允许重定向输出而无需在 Windows 上调用 shell,这是一种设置环境变量的可移植方式用于执行命令,并更好地控制 stdout 和 stderr 的重定向。相反,系统(和 Windows 上的 shell)允许调用任意命令行。 (来自ETHZ man page),另见R Manual
  • @Nana shell 基本上是在引入 system2 之前需要的仅限 Windows 的 hack。
猜你喜欢
  • 2013-04-15
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
相关资源
最近更新 更多