This chapter deals with start up files
It's quite easy to make an error and stop R working
it's also very easy to fix, just delete the file
When R starts up it will run
the site-wide R start up configuration file
.Rprofile
in the current working directory (the directory where R is
started on the command-line)
If that file doesn't exist, it will then check your home directory
Determine your home area:
path.expand("~")
We can again test whether this file exists using
file.exists("~/.Rprofile")
path.expand("~")
to determine your home directory and
file.exists("~/.Rprofile")
to see if you have an .Rprofile
.Rprofile
and add the linemessage("Today is ", Sys.Date())
options()
is a list that contains a number of default options
In my .Rprofile
file, I have the line
options(prompt="R> ", # Exciting new R> prompt digits = 4, # No. of digits displayed show.signif.stars = FALSE, # I hate stars fix_width = 88, # Useful under Linux Ncpus = 6L, # Parallel package installation continue = " ", # Hide the `+` mc.cores = 6L) # Parallel (see later)
r = getOption("repos") r["CRAN"] = "https://cran.rstudio.com/" options(repos = r) rm(r)
I also load a couple of useful functions.
Be very careful about loading functions by default; code is no longer portable.
setnicepar = function(mar=c(3,3,2,1), mgp=c(2,0.4,0), tck=-.01, cex.axis=0.9, las=1, mfrow=c(1,1), ...) { par(mar=mar, mgp=mgp, tck=tck, cex.axis=cex.axis, las=las, mfrow=mfrow, ...) }
It is perfectly acceptable just to put your functions in your .Rprofile
.
Downside: clutters up your work space.
If you run rm(list = ls())
, your functions will be deleted.
One neat trick is to combine hidden objects and environments.
When an object name starts with .
, by default it doesn't appear in
the output of the ls()
function
.obj = 1 ls()
This concept also works with environments. In our .Rprofile
file
we create a hidden environment
.env = new.env()
Then add our functions to the environment
.env$ht = function(d, n=6) rbind(head(d, n), tail(d,n))
At the end of the .Rprofile
file, we use the
attach()
attach(.env)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.