在R启动时运行R脚本 [英] Run R script with the start up of R
问题描述
我只想在启动R
时自动运行R脚本 example.r
我正在使用 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
可以通过运行 Sys来确定。 R会话中的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:
尽管在在此示例中,习惯上将此类代码包装在中。首先<-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屋!