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.

lmo
  • 37,904
  • 9
  • 56
  • 69
Emad
  • 279
  • 5
  • 22
  • 2
    You can add source("example.R") in your ~r installation path~/etc/rprofile.site – chinsoon12 Apr 28 '16 at 12:40
  • can you please explain more? – Emad Apr 28 '16 at 12:41
  • Where did you install your R? Go to that folder. Open a folder called etc inside that folder. inside the etc folder there is a file called rprofile.site. This file is sourced when r starts up – chinsoon12 Apr 28 '16 at 12:43
  • i went to /usr/lib64/R/etc/,,,,,,, and the files i found there are ,,,,,,,,javaconf ldpaths Makeconf Renviron repositories – Emad Apr 28 '16 at 12:45
  • You may need to create the file if it does not already exist. From your terminal, `echo 'source("/path/to/example.r")' >> /usr/lib64/R/etc/Rprofile.site`, assuming you have sufficient permissions. – nrussell Apr 28 '16 at 12:50
  • thank you @nrussell that's super clear ,, but is /usr/lib64/R is the default directory for R ? or maybe i went to the wrong dirc.? – Emad Apr 28 '16 at 12:56
  • On CentOS 7 it should be because that particular distribution is only available in 64-bit (i.e. it would not be in `/usr/lib/R`). If you hadn't previously created that file, it won't exist, but that is expected. From the help file: *"[...] ‘R_HOME/etc/Rprofile.site’, which is used if it exists (which it does not in a ‘factory-fresh’ installation)"*. You can always check the location of R by running `Sys.getenv("R_HOME")` in an R session. – nrussell Apr 28 '16 at 13:02
  • @nrussell thank you it's work just fine now, i would accepted as an answer – Emad Apr 29 '16 at 14:18
  • @nrussell can you please check my other question http://stackoverflow.com/questions/36935348/installation-of-package-forecast-had-non-zero-exit-status – Emad Apr 29 '16 at 14:44

1 Answers1

3

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")
}
nrussell
  • 18,382
  • 4
  • 47
  • 60