How to set a variable in .Renviron


Hello all ,
I am looking at a package called “rnoaa” and it suggests setting up the following:
• your .Renviron file with an entry like NOAA_KEY=your-noaa-token

 Can anyone explain how to append a variable to the .Renviron file ?

sys.setenv(NOAA_KEY = "Your-token")

This works


Check out ?Startup, but the usual way is to browse to your home directory on your computer, create a file called .Renviron then place your arguments in that. That way its available each time R starts, accessible using Sys.getenv().

The .Renviron file in your case would only read:


…but I put loads of stuff in there, such as other API keys.

Its useful to keep out sensitive info out of your code, such as your token, which is why its probably preferred over the way you can also set it, Sys.setenv()


usethis :package: has a useful helper function to modify .Renviron :

  • usethis::edit_r_environ() will open your user .Renviron which is in your home
  • usethis::edit_r_environ("project") will open the one in your project

You should look at how startup works in R. This post is useful: