【问题标题】:How to invoke script that uses scan() on Windows?如何在 Windows 上调用使用 scan() 的脚本?
【发布时间】:2012-10-27 13:24:44
【问题描述】:

如何调用如下的 R 脚本

scan()

在 Windows 中?当使用R 或Rscript 时,什么都不会被读取。使用Rscript 或littler(都在Linux 上),脚本按预期工作。

# Doesn't work because stdin is already redirected
R --no-save < test.R

# Works on Linux, doesn't on Windows
Rscript test.R

# Works on Linux, doesn't exist in Windows
r test.R

有什么方法可以在不更改 R 代码的情况下实现这一点?

可能相关:为什么 Windows 中没有 --interactive 开关?

【问题讨论】:

  • 请给我们一个可重现的例子。
  • @PaulHiemstra:完成,谢谢。
  • 这在R devel post 中进行了讨论。
  • 从未在 Windows 上尝试过(所以我不会将其作为答案发布)但包含 scan(file("stdin")) 的脚本可以在 Mac 上运行。
  • @plannapus 它确实适用于 Windows。如果您想获得积分,我会让您发布答案。您必须稍微修改它,以便使用 character 并且您必须点击 Ctrl+Z 才能结束扫描:input&lt;-scan(file("stdin"), what = character())

标签: windows r rscript


【解决方案1】:

因此,正如我们在 cmets 中讨论的并确认 @nograpes,您可以使用以下内容:

scan(file("stdin"), what=character())

在脚本中而不是 scan() 中,以便在命令行界面中执行脚本时以交互方式从标准输入中读取。
然后,您需要在 Windows 下按 Ctrl + Z 结束扫描(在 Mac 上为 Ctrl + D)。

【讨论】:

  • 为什么 Windows 和 Unix 的行为不同?
  • 我正在使用 Mac(所以是 Unix),我还必须提供 file("stdin")。我在 Linux 方面没有能力,所以我猜这方面真的帮不上忙。
猜你喜欢
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 2018-06-26
  • 2013-06-22
  • 1970-01-01
相关资源
最近更新 更多