Did you know that you can customize the startup and closing of R, using an .Rprofile file?
Using this file, you can automatically load packages at the start of each session, set options and environment variables as well as create startup messages. An example of an .Rprofile is
# Things you might want to change
default_opts = callr::r(function(){options()})
# options(papersize="a4")
# options(editor="notepad")
# options(pager="internal")
# set the default help type
# options(help_type="text")
# options(help_type="html")
# set a site library
# .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")
# set a CRAN mirror
# local({r <- getOption("repos")
# r["CRAN"] <- "http://my.local.cran"
# options(repos=r)})
# Give a fortune cookie, but only to interactive sessions
# (This would need the fortunes package to be installed.)
# if (interactive())
# fortunes::fortune()
.First <- function(){
msg = c(
" __ __ ___ __ ",
" /\\ \\ __/\\ \\ /\\_ \\ /\\ \\__ ",
" \\ \\ \\/\\ \\ \\ \\ __\\//\\ \\ ___ ___ ___ ___ __ \\ \\ ,_\\ ___ ",
" \\ \\ \\ \\ \\ \\ \\ /'__`\\\\ \\ \\ /'___\\ / __`\\ /' __` __`\\ /'__`\\ \\ \\ \\/ / __`\\ ",
" \\ \\ \\_/ \\_\\ \\/\\ __/ \\_\\ \\_/\\ \\__//\\ \\L\\ \\/\\ \\/\\ \\/\\ \\/\\ __/ \\ \\ \\_/\\ \\L\\ \\",
" \\ `\\___x___/\\ \\____\\/\\____\\ \\____\\ \\____/\\ \\_\\ \\_\\ \\_\\ \\____\\ \\ \\__\\ \\____/",
" '\\/__//__/ \\/____/\\/____/\\/____/\\/___/ \\/_/\\/_/\\/_/\\/____/ \\/__/\\/___/ "
)
msg2 = c(
" ..:::;;;;;;;;;;:.. ",
" ..:::;;;;;;;;;;;;;;;;;;;;:. ",
" .:::;;;;;;;;;;;;;;;;;;;;;;;;;;:. ",
" .:::;;;;;;;;;;::...........:;;;;;;:. ",
" ..::;;;;;;;;:. .:;;;;. ",
" .:;;;;;;;;.. ;XXXXXXXXXXXXXXXX+..;+;.",
" :;;;;;;;:. ;XXXXXXXXXXXXXXXXXX;.;+:",
" ;;;;;;;;. ;XXXXXXxxxxxXXXXXXXX:.++",
" ;;;;;;;; ;XXXXXX:. .+XXXXXX;.+;",
" :;;;;;;;. ;XXXXXX:. .xXXXXXX.;+:",
" .:;;;;;;;. ;XXXXXXXXXXXXXXXXXX:.+;.",
" .:;;;;;;;:. ;XXXXXXXXXXXXXXXX;.:+;. ",
" ..;;;;;;;;;:. ;XXXXXXXXXXXXXX+..+;.. ",
" ..:;;;;;;;;+xXXXXXX:;:;XXXXXXX.. ",
" .:;;++++xXXXXXX+ +xXXXXXXx.. ",
" ....:+XXXXXX;: ;XXXXXXX. ",
" ;XXXXXX:. .+XXXXXXx. ",
" ;XXXXXX:. .+XXXXXXX.."
)
for(i in seq_along(msg)) {
message("\r", msg[[i]])
Sys.sleep(0.005)
}
message("\n\n\n")
for(i in seq_along(msg2)) {
message("\r", msg2[[i]])
Sys.sleep(0.075)
}
message("\n\n\n")
cat("Are you excited for another day of programming in R? I sure am, so wake up and let us begin!!!\n\n")
startupScript = "~/StartupFunctions.R"
if(file.exists(startupScript))
source(startupScript)
if(Sys.getenv()["COMPUTERNAME"] == "RandomPC")
.libPaths("C:/Program Files/R/R-4.4.1/library")
else
.libPaths("//Server/R/R-4.4.1/library")
if("fortunes" %in% rownames(utils::installed.packages()) & interactive()) {
cat("\n\n Quote of the day: \n")
print(fortunes::fortune())
}
}
.Last <- function(){
cat("\nAre you leaving='(, noooooooooooooo, why, why?! I hope to see you back soon!\n")
}Here, you can see that we have two functions: .First()
and .Last(). The .First() function specifies
what R does when it starts and .Last() when you close
it.
When you inspect the .First() function, you will see
that I first print a message (lines 24 to 61). Hereafter, I source a
script which contains a bunch of custom functions that I wrote over the
years (lines 66 to 68). On lines 69 to 72, I change the location of the
library (i.e., where the packages are installed) depending on which PC I
am using. This can be especially useful if you are working on the cloud
and have a different library location. Lastly, I print a quote of the
fortunes package (lines 73 to 76). This will give you a
random quote about (using) R every time you start R and it was quote 168
which motivated me to write this post:
##
## When talking about user friendliness of computer software I like the analogy of
## cars vs. busses: [...]
## Using this analogy programs like SPSS are busses, easy to use for the standard
## things, but very frustrating if you want to do something that is not already
## preprogrammed.
## R is a 4-wheel drive SUV (though environmentally friendly) with a bike on the
## back, a kayak on top, good walking and running shoes in the passenger seat, and
## mountain climbing and spelunking gear in the back. R can take you anywhere you
## want to go if you take time to learn how to use the equipment, but that is
## going to take longer than learning where the bus stops are in SPSS.
## -- Greg Snow
## R-help (May 2006)
This file can either be at the user (i.e., the file is used for each R session) or project level (i.e., the file is only used for a specific project). In addition, if you have an .Rprofile at the project level, R will only use this one. The user-level .Rprofile is located in the base of the user’s home directory (C:/Users/…/Documents/ on Windows) and the project-level .Rprofile files live in the base of the project directory (the working directory).
The .Rprofile file is sourced as regular code. Hence, if you want to
include the general, user level .Rprofile, you can do so by including
source("~/.Rprofile") in your project level .Rprofile. If
you want to set environment variables, you have to do so inside a
Sys.setenv(key = "value") call.
To edit your .Rprofile, you can run the
usethis::edit_r_profile() function from within an R
session. You can specify whether you want to edit the user or project
level .Rprofile.
One last thing to keep in mind is that, if you have a custom .Rprofile file, these customizations are saved into an .Rdata file when you create one. Hence, if you share this .RData file, your colleague(s) will also see your startup message if you have one. Please keep this in mind if you have some funny startup messages (I, for example, had an unfortunate quote of a comedy movie), since this will also be shared. In addition, if you decide to automatically load certain packages at startup, this may not work if your colleague does not have these packages installed.
To make sure that this does not happen, you can run the following code
By running this code, you overwrite the existing
.First() and .Last() functions. Additionally,
we make use of the saved default options (saved in the object
default_ops, see line 1 of the example .Rprofile) to reset
all the options to their default values.