3MW (Environment Variables in R)

Enjoy 3 Minute Wednesdays at no cost.

This newsletter is brought to you for free. If you want to advance your R skills and support my work at the same time, then you will like my paid offerings:

Guten Tag!

Many greetings from Munich, Germany. Environment variables control system-wide or session-wide settings inside your R session. This sounds incredibly boring. And, honestly, it kind of is.

But it can feel complicated to R users who haven’t encountered them before. When you do bump into environment variables “in the wild,” they can seem unnecessarily tricky.

That’s why, in today’s newsletter, I’ll cover a bit of the mundane stuff. That is, I’ll show you how to set and get environment variables in R. I know: Exciting! So let’s dive in.

When Do Environment Variables Matter?

Let’s quickly look at a couple of real-world examples where environment variables show up in R workflows:

Using APIs (like OpenAI)

If you’re working with APIs, such as using the {ellmer} package to connect to ChatGPT or other AI services, you’ll need to store your API keys as environment variables.

For example, to use OpenAI’s API, you need to specify the environment variable OPENAI_API_KEY. This keeps your credentials secure and avoids hard-coding sensitive infos into your scripts.

Deploying Shiny Apps

If you’re deploying a Shiny app, you might want to keep different settings for:

  • Production (the live version of your app that your users see)

  • Development (the version of the app that you as a developer see and use to test new features)

Environment variables let you switch configurations easily depending on your deployment stage (without editing the core code.)

So with the examples out of the way, let’s dive into three popular methods for setting environment variables.

Method 1: .Rprofile (My Favorite)

The .Rprofile file allows you to run R code automatically at the start of each R session. If your project doesn’t have such a file, just create a text file and name it .Rprofile. And to set an environment variable, you can use Sys.setenv() inside the .Rprofile file:

🚨 NOTE: Only use Sys.setenv() inside your .Rprofile. If you put it in a regular script, it might get saved to your R history file. And if that ever gets pushed to a public repo, your API key could be compromised.

Method 2: .Renviron

The .Renviron file is simpler. Unlike .Rprofile, it cannot execute R code as it only holds key-value pairs. But just like .RProfile, these environment variables are always loaded at the start of an R session.

The file itself always follows the syntax key=value. Here’s an example:

Method 3: .env (via {dotenv})

The .env file is popular in many programming languages (like Python). A .env file looks just like .Renviron. The difference is only that you load the .env file manually. In R, you can use the {dotenv} package to do that.

This method is especially handy if you want to share environment variables across R and Python.

There you have it. Three ways to work with environment variables in R:

1️⃣ .Rprofile (using Sys.setenv())
2️⃣ .Renviron (key-value only)
3️⃣ .env (loaded via {dotenv})

Sure, it’s not the flashiest topic but it’s super important once you move into production-like environments or need to store secrets. As always, if you have any questions, or just want to reach out, feel free to contact me by replying to this mail or finding me on LinkedIn or on Bluesky.

See you next week,
Albert 👋

Enjoyed this newsletter? Here are other ways I can help you:

Reply

or to participate.