env_vars | R Documentation |
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.
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:
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.)
You should see a popup box with two panes; "User variables for your_username" on top and "System variables" on bottom.
Click "New..." under the "User variables..." section.
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".
The variable should now appear in the "User variables..." pane. You can click "OK" to exit the window.
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.
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.