如何在 Windows 上调用使用 scan() 的脚本? [英] How to invoke script that uses scan() on Windows?
问题描述
如何调用如下所示的 R 脚本
How to invoke an R script like the following
scan()
在 Windows 中?使用 R
或 Rscript
时,不会读取任何内容.使用 Rscript
或 littler
(均在 Linux 上),脚本按预期工作.
in Windows? When using either R
or Rscript
, nothing is read. With Rscript
or littler
(both on Linux) the script works as expected.
# 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 代码的情况下实现这一目标?
Is there any way at all to achieve this without changing the R code?
也许相关:为什么Windows中没有--interactive
开关?
Perhaps related: Why is there no --interactive
switch in Windows?
推荐答案
因此,正如我们在评论中所讨论的,并经过@nograpes 的确认,您可以使用以下内容:
So as we discussed in the comments and with confirmation of @nograpes, you can use the following:
scan(file("stdin"), what=character())
在脚本中而不是 scan()
中,以便在命令行界面中执行脚本时以交互方式从标准输入读取.
然后您需要点击 Ctrl + Z 结束 Windows 下的扫描(Ctrl + D 在 Mac 上).
in a script instead of scan()
to read interactively from standard input when the script is executed in the command-line interface.
You then need to hit Ctrl + Z to end scanning under Windows (Ctrl + D on a Mac).
这篇关于如何在 Windows 上调用使用 scan() 的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!