Connecting to the GitHub to install packages from a private GitHub repository requires security set ups and this blog details how to do it (and how not to do it).
Because some of the packages we are using are (currently) private repositories it is not possible to build using just {remotes} as a Personal Access Token, specifically a GITHUB_PAT, is needed. The {usethis} package suggests using the local Git credential store but this wasn’t compatible with some of our team’s set ups so later in this blog are details on how to do it the “inadvisable” way to be used at your own discretion.
This part is the same for whatever method you use on your own computer as this section details how to get the GITHUB_PAT set up. To check for existing GITHUB_PAT use code:
Sys.getenv("GITHUB_PAT")
If nothing has been set up by you on GitHub this may return an empty string ““.
Firstly, go to https://github.com/settings/tokens which takes you to your own personal GitHub account settings where you will need to Generate new token
. Give this a suitable name like Repo access
and tick the repo
group for full control of private repositories.
Remember to copy the code that is generated as this cannot be viewed again
install.packages("usethis")
install.packages("gitcreds")
install.packages("remotes")
{usethis} suggests using the local Git credential store quite strongly!
If you have previously set your GitHub PAT in .Renviron, stop doing that.
GitHub will open and, if you are not already logged in, you will need to enter your (GitHub) password.
The page for GitHub tokens management is https://github.com/settings/tokens and you can do the same as the code
in this screen by selecting Generate new token
. Give this a suitable name like Package installation
and tick the repo
group for full control of private repositories.
Remember to copy the code that is generated as this cannot be viewed again
Next set up a GITHUB_PAT
in RStudio:
::gitcreds_set() gitcreds
You will then be prompted to enter the copied token (no quotes are required). If you’ve already got a token entered the following message will show:
-> Your current credentials for 'https://github.com':
protocol: https
host : github.com
username: PersonalAccessToken
password: <-- hidden -->
-> What would you like to do?
1: Keep these credentials
2: Replace these credentials
3: See the password / token
Selection: ```
Enter an item from the menu, or 0 to exit
Selection:
Reference: https://usethis.r-lib.org/articles/articles/git-credentials.html
The {usethis} documentation suggests using {pak} but this may not work on network drives/VPNs (there are a few issues that have been opened and quickly closed referring to these as potential problems) and so in the meantime combining {remotes} with {gitcreds} is a workaround:
# install.packages("remotes")
::install_github("<name of GitHub account>/<name of repository>",
remotesauth_token = gitcreds::gitcreds_get(use_cache = FALSE)$password)
install.packages("usethis")
install.packages("remotes")
If you are sure this is what you want to do then type in RStudio:
::edit_r_environ() usethis
In the file that opens, type into it (where YOUR-PAT is the generated code you’ve copied) and save the file:
=YOUR-PAT GITHUB_PAT
Restart R and run Sys.getenv("GITHUB_PAT")
to check that the ""
has changed.
Reference: https://www.jumpingrivers.com/t/2019-user-git/03-githubpat.html#6
Now, for as long as the token exists, the following code will install the package:
# install.packages("remotes")
::install_github("<name of GitHub account>/<name of repository>", auth_token = Sys.getenv("GITHUB_PAT")) remotes
It is preferable to build from the repository rather than locally as this should be what everyone in the team has access to. To build and test any new functions use Ctrl+Shift+L
from the {devtools} package to load locally for the session.
If you see mistakes or want to suggest changes, please create an issue on the source repository.
For attribution, please cite this work as
Turner (2021, Dec. 17). CDU data science team blog: Personal Access Tokens. Retrieved from https://cdu-data-science-team.github.io/team-blog/posts/2021-12-18-personal-access-tokens/
BibTeX citation
@misc{turner2021personal, author = {Turner, Zoë}, title = {CDU data science team blog: Personal Access Tokens}, url = {https://cdu-data-science-team.github.io/team-blog/posts/2021-12-18-personal-access-tokens/}, year = {2021} }