Already in the first chapter, we showed you how to set up RStudio with the built-in settings so that it corresponds, practically and visually, with your wishes and preferences. But, there are also other options that cannot be set within the RStudio GUI since they are general R settings. An example of this is the Rprofile. But everything you determine in your personal Rprofile will also be executed in RStudio.
You may have never heard of the so-called Rprofile before. In fact, at first glance, it is just a normal text file, which can be found in the R home directory. But it could also be possible that you do not have such a file.
The customization of Rprofile is an interesting way to load personally stored functions, options, scripts, the preferred CRAN mirror, and so on, for your R sessions at every startup of R, respectively RStudio.
For Windows environment to change your Rprofile on Windows, navigate to the …\R\R-Version\etc
folder and edit the file, Rprofile.site
. Next, restart R and you are done.
For Mac and Linux, first you need to create the Rprofile file. Create a new file named Rprofile.txt
. This can be done with any text editor. Open your terminal and enter the following: cp Rprofile.txt .Rprofile
. By doing so, the profile file becomes invisible, which is the standard case for such files. Then, restart R.
Since the possible features that you can integrate into your personal Rprofile are endless, in the following sections, we will discuss only a few examples.
As the name implies, by using these functions, it can be defined what happens first, and what happens at the end of your R session. For example, you can define that some libraries should be loaded at the startup, and further, write a custom startup message:
.First <- function(){ library(ggplot2) library(stats) library(htmlwidgets) library(shiny) cat("\nHey there, welcome back! Let's write some great R code.") }
In the same way you, can deal with the .Last
function. For example, save your whole R command history and print a goodbye message:
.Last <- function(){ if (!any(commandArgs()=='--no-readline') && interactive()){ library(utils) try(savehistory(Sys.getenv("R_HISTFILE"))) } cat("\nIt was a long day. Goodbye at ", date(), "\n") }
local({r <- getOption("repos") r["CRAN"] <- "https://cran.rstudio.com/" options(repos=r)})
# turn debugging on or off # place "browser(expr = isTRUE(getOption("debug"))) # BROWSER" in your function # and turn debugging on or off by bugon() or bugoff() bugon <- function() options("debug" = TRUE) bugoff <- function() options("debug" = FALSE) #pun intended
Preceding example is taken from: http://stackoverflow.com/posts/7107100/revisions.
Using aliases:
#ht==headtail, i.e., show the first and last 10 items of an object ht <- function(d) rbind(head(d,10),tail(d,10)) # Show the first 5 rows and first 5 columns of a data frame or matrix hh <- function(d) d[1:5,1:5]
Preceding example is taken from: http://stackoverflow.com/posts/8676073/revisions.
Set a proxy for your web requests:
Sys.setenv(http_proxy="http://XXX.xxx.x.xxx:xx/")
As already stated, you can almost define and set everything that you can imagine in your personal Rprofile. However, where a lot of light is, there is also shade. If you often collaborate with other people on R projects, or when you share your R scripts elsewhere, a big problem can arise with an intensely customized Rprofile: reproducibility. There can be several settings that you have added, which other users do not see, and your scripts may fail.
You can check this if you start R in your terminal with the command, --no-init-file
. If your scripts are still working, you can share them without having to worry.