Open In App

Managing Secrets and Auth Details in R Language

Improve
Improve
Like Article
Like
Save
Share
Report

Let us assume that we have an R project or an R script where we are required to use some sort of API key or secret to generating a token. Often when we are collecting data from the web using APIs, we are required to use an API key and some other secret information to uniquely identify ourselves to the servers. Also, sometimes apart from keys, there may be certain segments of our code that we do not wish to disclose.

A Step-by-Step Guide

In this article, let’s discuss how we can use our personal API keys as well as host our project on Github without exposing the API secrets.

Step 1: Create a new repository where you intend to store your scripts and clone them on your local machine. Even if a repository already exists, simply clone it.

Step 2: If you directly cloned your repository onto your local machine, you’re all set to proceed and if not, initialize your local machine folder and add a remote to it to set up your local machine folder. We’ll refer to this folder simply as a folder throughout this article.

Step 3: Create a new .R file in the folder from step 2 and name it whatever you like. I like to name it secret.R. Now, open this secret.R and store all your secret information (keys) in here. Something like this and set the variables in accordance with the secrets assigned to you.

Step 4: In the main script/file where you’re working and where these values actually have to be used, use the source() function to access all the variables. That would look something like this and Ignore the Environment window. Notice the use of the source() function and its variables.

Step 5: After you are done with the required coding and it’s time to push your code to your Github repository create a new file called .gitignore. Make sure that you name this file exactly the same.

Step 6: Open the .gitignore file in your text editor and then simply type the name of the secret.R file in it. Something like this:

This .gitignore file should be in the same folder as step 2.

Step 7: Push your code to your public GitHub repository. Now when you open this repository on Github, you won’t see the secret.R file listed in the repository. That’s it. Now go ahead and code without any security concerns. 


Last Updated : 12 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads