How to set a variable in .Renviron


#1

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 ?

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

This works


#3

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:

NOAA_KEY=your-noaa-token

…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()


#4

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: