Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run R script with the start up of R

Tags:

r

centos

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.

like image 581
Emad Avatar asked Nov 26 '25 22:11

Emad


1 Answers

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.

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")

the changes will be reflected in a new R session:

enter image description here


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")
}
like image 143
nrussell Avatar answered Nov 28 '25 17:11

nrussell