A simple, step-by-step guide to setting up an .Rprofile file to automatically load your data and customize your R environment.
As someone committed to reproducibility, I often found myself starting with a clean slate every time I reopen my R projects. This meant reloading my data from scratch each time, which was time-consuming and disrupted my workflow.
The breakthrough came when I stumbled upon a cheat sheet for best practices in R by Jacob Scott. It introduced me to the concept of the .Rprofile file—a script that automatically runs R code on startup. My brain lit up with possibilities!
Create a new text file by clicking File -> New File -> Text File.
Save it as .Rprofile
(no other names allowed). Note that it starts with a dot.
Create a script (load_data.R) that contains the code to load your data into the environment. This can be CSV, RDS, or any other format.
In your .Rprofile, add the line to source your data loading script:
source(here::here("load_data.R"))
Save the .Rprofile file in the root directory of your project.
Now, every time you open R, your data will be loaded automatically. You can also add other scripts to your .Rprofile to customize your R environment further.
I wait until I Finish my data cleaning and wrangling before writing my .Rprofile to ensure I know exactly what data needs to be loaded.
Recently I started loading my customized themes on startup using the same .rprofile. Remember, you don’t have to create a new .Rprofile file for every script or setting you want to load within the same project. You can use the same .Rprofile to include all your loading scripts and customizations.
If you find yourself using library(tidyverse) and library(here) every time you start your projects (which I do), then it’s imperative to add them to your .Rprofile so that they load automatically.
I hope this helps you as much as it helped me. Happy coding!
For attribution, please cite this work as
Soundararajan (2024, July 2). My R Space: Discovering the Power of .Rprofile in My R Workflow. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2024-07-02-discovering-the-power-of-rprofile-in-my-r-workflow/
BibTeX citation
@misc{soundararajan2024discovering, author = {Soundararajan, Soundarya}, title = {My R Space: Discovering the Power of .Rprofile in My R Workflow}, url = {https://github.com/soundarya24/SoundBlog/posts/2024-07-02-discovering-the-power-of-rprofile-in-my-r-workflow/}, year = {2024} }