可执行.R 文件未运行,路径被忽略



我试图让helloworld.R运行。我已经给了它可执行权限,并在文件顶部添加了#!/usr/bin/Rscript。(我也尝试过#!/usr/bin/en Rscript(。这是一个相对简单的文件:

#!/usr/bin/Rscript
x <- rnorm(100)
df <- data.frame(x = x)
write.csv(df, "sim_data.csv")

但是,任何运行该文件的尝试都会遇到以下错误

ARGUMENT '/home/path/to/file/helloworld.R' __ignored__
R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
(...)

令人惊讶的是,将文件重命名为 helloworld.py 可以让它运行良好(并输出sim_data.csv其预期内容(,但这似乎很hack,所以我想知道是否有更好的解决方案。

错误消息指示运行的可执行文件是R可执行文件,而不是Rscript可执行文件。您可以使用终端进行验证:

R  /home/path/to/file/helloworld.R         # produces above error
Rscript /home/path/to/file/helloworld.R    # should work
/home/path/to/file/helloworld.R            # should work if file is executable

很可能您的窗口管理器或桌面环境具有与R可执行文件关联的.R文件,因此当您单击此类文件时R启动。如何更改取决于正在使用的窗口管理器或桌面环境。

你没有

运行 RScript,你运行的是 R,所以让 R 告诉你运行一个文件:

$  R --help
Usage: R [options] [< infile] [> outfile]
   or: R CMD command [arguments]
Options:
  --args                Skip the rest of the command line
  -f FILE, --file=FILE  Take input from 'FILE'
  -e EXPR               Execute 'EXPR' and exit

所以试试这样:

R -f helloworld.R

问题是,为什么您的Rscript可执行文件被窃听并指向R

最新更新