启动 R 运行 R 脚本 [英] Run R script with the start up of R
问题描述
我只想在启动 R 时自动运行 R 脚本 example.r
我在 centOS
上使用 R 版本 3.2.3
I just want to run an R script example.r
automatically when I start R
I am using R version 3.2.3
on centOS
我搜索了这个,但我无法弄清楚.
I searched for this but I couldn't figure that out.
推荐答案
R的启动行为可以通过编辑Rprofile.site
来修改,可能需要创建,作为默认的R安装不会自动为您执行此操作.在 CentOS 7 上,此文件应位于目录 /usr/lib64/R/etc/
中;或更一般地说是 $R_HOME/etc/
,其中 R_HOME
可以通过从 R 会话运行 Sys.getenv("R_HOME")
来确定.
The startup behavior of R can be modified by editing Rprofile.site
, which may need to be created, as the default R installation does not do this for you automatically. On CentOS 7, this file should be located in the directory /usr/lib64/R/etc/
; or more generally $R_HOME/etc/
, where R_HOME
can be determined by running Sys.getenv("R_HOME")
from an R session.
例如,如果我修改我的 Rprofile.site
如下,
For example, if I modify my Rprofile.site
as follows,
[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")
更改将反映在新的 R 会话中:
the changes will be reflected in a new R session:
虽然在这个例子中显然没有必要,但习惯上将这样的代码包装在 .First <- function() { ... }
以确保它在会话启动时立即运行:
Although apparently not necessary in this example, it is customary to wrap such code in .First <- function() { ... }
to ensure that it is run immediately upon session startup:
[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 运行 R 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!