Skip to content
Home » Customizing R: the .Rprofile file

Customizing R: the .Rprofile file

Tags:

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:

  • if you create a project-wide .Rprofile file, R will not source the code that is stored in the user-wide .Rprofile file.
  • your .Rprofile file should end with a Nextline character, or the last line of your file will not be sourced.
  • because .Rprofile files are loaded in when R starts, make sure to restart R every time you add a new environment variable.

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.

22 thoughts on “Customizing R: the .Rprofile file”

  1. Being а Linear Intense Phased Array Transduxer ᥙser, І am very impressed
    witһ the capabilities оf tһіs device. Ꭲhіs trahsducer iѕ
    especіally beneficial for scanning lаrge
    ɑreas, measurig pipe thickness, oor examiningg
    boards аnd sticks.

  2. Kerassentials are natural skin care products with ingredients such as vitamins and plants that help support good health and prevent the appearance of aging skin. They’re also 100% natural and safe to use. The manufacturer states that the product has no negative side effects and is safe to take on a daily basis. Kerassentials is a convenient, easy-to-use formula.

  3. Pingback: URL

  4. Attractive section of content. I just stumbled upon your site and in accession capital to assert that I get actually
    enjoyed account your blog posts. Any way I’ll be subscribing to your
    augment and even I achievement you access consistently quickly.

  5. You can certainly see your expertise within the work you write.
    The arena hopes for more passionate writers such as you who aren’t
    afraid to mention how they believe. Always go after your heart.

  6. I’ve been exploring for a little bit for any high-quality articles or blog posts on this
    kind of house . Exploring in Yahoo I finally stumbled upon this website.
    Studying this info So i am satisfied to exhibit that I have an incredibly just right uncanny feeling I
    found out just what I needed. I such a lot undoubtedly will make sure to do not disregard this website and provides it a glance
    on a relentless basis.

  7. Pingback: Angthong National Marine Park

Leave a Reply

Your email address will not be published. Required fields are marked *