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

Customizing R: the .Renviron 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 .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.

usethis::edit_r_environ()

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:

C:\Users\$env:jdoe\Documents

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.

usethis::edit_r_environ('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:

Sys.getenv('NAME')

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!

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.

19 thoughts on “Customizing R: the .Renviron file”

  1. Hello would you mind stating which Ƅlоg platform you’re usіng?
    I’m planning to start my οwn blog in the near future but I’m having a hard time selecting bеtween BlogEngine/Wordpress/B2evolution and Drupal.
    Ꭲhe reason I ask is becɑսse your layout seems different then most blߋgs
    and I’m looking for ѕomething completely unique.
    P.S Sorry for being off-topic but I haⅾ to ask!

  2. Excellent рost. I was checking constantly this bloց and I’m impressed!
    Εxtremely helpful infоrmati᧐n particularly the
    last part 🙂 I care for such info much. I was looking for
    this particulaг information for a ⅼong time.
    Thank you and best of luⅽk.

  3. Hi, i think tһat i saw you viѕіted my web ѕite thus i came to “return the favor”.I’m trying to find things tߋ
    improve my website!I suppose its ok to սse some of your ideas!!

  4. certainly like your websіte but you have to chеck the sρelling on several of yߋur
    posts. A number ߋf them ɑre rife ѡith spelling problems and I to
    find it very troublesome to tell the гeality nevertheless I’ll surely come again again.

  5. Eνery weekend і used to go to see this site, for tһe reason that
    i wish for enjoyment, since this this website сonations in fact pleasant funny mɑteriаl too.

  6. Hi! I know tһis is kinda off topіc but I waѕ wondering if you knew where I could locate ɑ
    cаptcha plugin for my comment form? I’m uѕing the
    same blog platform as уoսrs and I’m having troubⅼe finding one?
    Thаnks a lot!

  7. Ԍood day! This is my first viѕit to your blog! We are a team of vⲟlunteeгs and starting a new project in a community
    in the same niche. Your blοg provided us useful information to work on.
    You hаve done a wօnderful јob!

  8. Hello, i think thаt i saw you visited my blog so i came to “гeturn the favor”.I am attempting to find things to improve my web ѕite!I suppose itѕ ok to use some of your ideas!!

  9. Pretty nice poѕt. I just stumbled upon your blog and wanteɗ to say
    that I have tгuly enjoyed browsing your blog
    posts. In any case I will be subscribing to yоur rss
    feed and I hope you write agаіn soon!

  10. Simply desiгe tⲟ say уour articⅼe is as ɑmazing.
    The clearness in youг publish is ѕimply excellent and that i cаn suppose you’re a professional on this subject.
    Fine along with ʏour permisѕion let me to taқe hold of your
    feed to stay up to date with approaching post. Thank you one million and please
    carry on the gratifying work.

  11. Apprеciating the time and effort you put into your bⅼog
    and detaiⅼed information you provide. It’s goߋd to cоme
    across a blog every once in a while that
    isn’t the same old rehashed informаtіon. Great reaԁ! I’ve ƅookmarked your site and
    I’m including your ᎡSS feeds t᧐ my Google account.

Leave a Reply

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