env_vars: How to Access Sensitive Information Non-Interactively with...

env_varsR Documentation

How to Access Sensitive Information Non-Interactively with Environment Vars

Description

When writing code that might be shared, it's bad practice to hard-code sensitive information in a script (or package, obviously). For data that doesn't need to be shared, but doesn't necessarily need encryption, environment variables provide a way to store and retrieve information.

Details

Environment variables are just variables that are available using the Sys.getenv() function. They're not specific to R; your Windows software uses a number of environment variables to function. They're particularly useful for storing information that you need to access from multiple places, or information that you don't want to leave your computer, like API keys. You can also store usernames and passwords in this file; it's better than entering them directly, but note that they will not be encrypted. A better solution for usernames, passwords, and other things that should be encrypted is the keyring package; however, the coviData package does not currently follows this convention.

To use environment variables, follow these steps:

  1. Go to the Windows search bar and type "environment variable". You should see an option to "Edit environment variables for your account". Open it. (Note that the option "Edit the system environment variables" requires admin rights; you can't use it.)

  2. You should see a popup box with two panes; "User variables for your_username" on top and "System variables" on bottom.

  3. Click "New..." under the "User variables..." section.

  4. This will open an input box with fields "Variable name" and "Variable value". These fields correspond to the variable name and value in R. Type the name and value of your variable in each field, and click "OK".

  5. The variable should now appear in the "User variables..." pane. You can click "OK" to exit the window.

  6. To see a new variable in R, you need to restart the sesson. Save your work, then go to Session -> Restart R in the top left corner of R Studio. Alternately, you can close R Studio and re-open it.

  7. Type Sys.getenv("your_variable") into the console and press Enter. R should return your variable value!

coviData currently uses environment variables for REDcap API tokens, but may expand to ESSENCE and NBS tokens as well in the future.


jesse-smith/coviData documentation built on Jan. 14, 2023, 11:08 a.m.