Environment Variables in RStudio on Mac

I recently asked a question on Stack Overflow on the best way to set environment variables on a Mac for use within an RStudio session.

It wasn’t as straightforward as I would have thought, so I wanted to share this quick post as a way to remind my future self of a quick way to solve the issue.

Overview

Generally, you can set environment variables by:

export YOUR_VAR=abc123

within a terminal. In a new session,

echo $YOUR_VAR

should yield what you need.

Within python, you can get at it by:

import os
os.getenv("YOUR_VAR")

but if you are using R and Rstudio, Sys.getenv("YOUR_VAR") will return "".

No bueno.

A solution

Navigate to ~, and create the .Renviron file if it doesn’t already exist

cd ~
touch .Renviron
open .Renviron

And in the file, type

YOUR_VAR="abc123"

Save the file and restart/reopen Rstudio.

From there, Sys.getenv("YOUR_VAR") should be good to go.

Deeper Dive

For a more granular look at this functionality, feel free to reference the links below

Brock Tibert
Brock Tibert
Lecturer, Information Systems

Lecturer in Information Systems, Consultant, and nerd.

Related