Site icon Roel Peters

Customizing R: the .Rprofile file

In this 2-part blog post I want to discuss two files that allow you to customize your R experience. In this part I discuss the .Rprofile file.

What is the .Rprofile file?

The .Rprofile file runs right after the .Renviron file (see part 1). Unlike the .Renviron file, you can actually put real R code in it. That means it is a highly recommended way to customize your R interface. I also use it to load packages that I tend to use in every project, such as data.table and ggplot2.

You can create a user-wide .Rprofile file that is accessible in every R project on your machine, or you can create a project-wide file that is only available in the project you created the file for.

There’s also a Rprofile.site file, but I’m not discussing it in this blog post. If you want to know more, here is the documentation.

Where is the .Rprofile file located?

If you haven’t used the .Rprofile file yet, you probably have to create it. To make a user-wide .Rprofile file, you can run the following command. This will create the file and open it in the R(Studio) editor.

usethis::edit_r_profile()

In Windows, the file will be stored in the following directory:

C:\Users\$env:jdoe\Documents

If you want your file to be only sourced in a specific project, you should open your project and run the following command in the terminal:

usethis::edit_r_profile('project')

The file should be located in the folder of your project.

How does the .Rprofile file work?

The .Rprofile file is just like an .R file. You can add code to it and it will run, directly after sourcing the .Renviron file. However, there are two functions that are specifically defined for when you start R and when you quit R: respectively the .first and the .last functions.

In the following example file, I disable scientific notation and load two packages with suppressed messages. I even created a custom function for this: silentLoad. I also added a goodbye message for when I close R(Studio).

options(scipen=999)

silentLoad <- function(p) {
  cat(paste('Loading package:',p,'\n'))
  suppressWarnings(suppressPackageStartupMessages(library(p, character.only = T)))
}

.First <- function() {
  silentLoad('data.table')
  silentLoad('ggplot2')
}

.Last <- function() {
  cat('Bye now.')
}

You should keep in mind three things:

Finally, there’s some weird behavior when you source your .Rprofile and edit it in a new session. Some old deleted functions (even .first and .last) might still be active, because they are stored in the .Rdata file.

There’s a lot more to customize in R:

What’s the catch?

Just like with the .Renviron file, you’re customizing R to work for you. However, a good data scientist makes everything he does reproducible. In normal circumstances, you’re not going to share your .Rprofile file with someone else. In this scenario, there’s a good chance that your code is no longer reproducible. So make sure to document your code well.

You can also see what happens when you run your code without project-wide .Rprofile files, by checking the follow options in RStudio under Tools > Project Options…

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Great success!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Exit mobile version