GitHub Personal Access Token

If you want to use the GitHub API, essentially with the packages usethis and rcompendium, you need to create a GitHub Personal Access Token (PAT).

Proceed as follow:

  1. Go to https://github.com/settings/tokens
  2. Click on Generate new token (classic)
  3. Choose a name for your new token (e.g. your laptop name)
  4. Select no expiration date
  5. In the section Select scopes, select at least repo and workflow
  6. Copy this token

Then open RStudio Desktop and run:

## Install usethis package ----
install.packages("usethis")

## Open ~/.Renviron file ----
usethis::edit_r_environ()
# • Modify '~/.Renviron'
# • Restart R for changes to take effect

The last command will open the ~/.Renviron file. Add this new line: GITHUB_PAT='ghp_XXX' where ghp_XXX is your token. Do not forget to restart RStudio Desktop.

Finally let’s tell usethis to use the SSH protocol to communicate with GitHub. Open RStudio and run:

## Open ~/.Rprofile file ----
usethis::edit_r_profile()
# • Modify '~/.Rprofile'
# • Restart R for changes to take effect

In the ~/.Rprofile file, add this new line: options(usethis.protocol = "ssh"). Do not forget to restart RStudio Desktop.

To go further
  • The .Renviron file is a good place to store secrets (API tokens, password, etc.). This file is read by at startup. To access these variables, you can use the function Sys.getenv("GITHUB_PAT").

  • The .Rprofile file allows you to run command lines at startup. You can customize the default behaviour of and define some variables with options(). To access these variables, you can use options()$"usethis.protocol".

More information here.