我希望在启动 R 时自动运行一个 R 脚本 example.r
。
我的操作系统是 CentOS
,使用的是 R 3.2.3
版本。
我已经搜索过相关信息,但是没有找到合适的解决方案。
Rprofile.site
来修改,可能需要创建此文件,因为默认的R安装不会自动为您执行此操作。在CentOS 7上,此文件应位于目录/usr/lib64/R/etc/
中;或更一般地说,位于$R_HOME/etc/
,其中R_HOME
可以通过从R会话中运行Sys.getenv("R_HOME")
来确定。Rprofile.site
修改如下:[nathan@xxx] cat /tmp/example.r
x <- 1.5
y <- 2.5
z <- 3.5
t <- Sys.time()
[nathan@xxx] cat /usr/lib64/R/etc/Rprofile.site
options(prompt = "R> ")
options(continue = " ")
options(stringsAsFactors = FALSE)
options(scipen = 4)
source("/tmp/example.r")
.First <- function() { ... }
中以确保它在会话启动时立即运行。[nathan@xxx] cat /usr/lib64/R/etc/Rprofile.site
options(prompt = "R> ")
options(continue = " ")
options(stringsAsFactors = FALSE)
options(scipen = 4)
.First <- function() {
source("/tmp/example.r")
}
r安装路径/etc/rprofile.site中添加source("example.R")。 - chinsoon12echo 'source("/path/to/example.r")' >> /usr/lib64/R/etc/Rprofile.site
。 - nrussell