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 .Renviron file. First I answer what it does, where you can find it and finally we get to the part where we actually use it.
What is the .Renviron file?
The .Renviron file is a great way to store sensitive information such as passwords or API keys. There are two types of .Renviron files, and they are defined by their scope. First, there is the user-wide .Renviron file. All the information is this file will be accessible in R, no matter which project you’re working in. However, you can also create files that are only accessible in a specific project.
All the information in these files are stored as environment variables. It’s a common thing in computer science: a value pair usually set outside the program, often built into the operating system.
There’s also a Renviron.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 .Renviron file?
The .Renviron file is not necessarily created by default. The easiest way to create it and edit it, is by running the following command in R. Note that this will create a user-wide .Renviron file.
This command uses the usethis package, which is “a workflow package: it automates repetitive tasks that arise during project setup and development”. Once you run it, it will created a file in a directory. In Windows, this directory is:
The variables in this .Renviron file are now accessible in all your projects. If you want to create an .Renviron file that is only accessible in a specific project, you can run the following command in the session of your project.
The file will be stored in the folder of your project and its environment variables you put in it will only be accessible in this specific project.
How does the .Renviron file work?
Actually, it’s really easy. You can assign values to variables using the = operator. You don’t necessarily need to add double quotes, but it is good practice if your value contains spaces. Here’s an example:
NAME="Roel Peters" GCP_KEY=x1y2z3
In R, you can access the environment variables by using the Sys.getenv() function. If you don’t add an argument to the function, it will return all the environment variables. To get access to NAME, you do:
This will return Roel Peters, the value we set in the .Renviron file.
You should keep this in mind:
- if you create a project-wide .Renviron file, you will not have access to the environment variables that are stored in the user-wide .Renviron file.
- Your .Renviron file should end with a Nextline character, or the last line of your file will not be sourced.
- Because .Renviron files are loaded in when R starts, make sure to restart R every time you add a new environment variable.
What’s the catch?
A good data scientist makes every little thing he/she does reproducible. That means that if you use environment variables, and they are not available on the system of someone else who tries to run your code, it’s not reproducible. Make sure to properly document your code to avoid misunderstandings.
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!