Robert's Data Science Blog

Install R Packages from VSTS

Installing R packages from popular non-CRAN location is easy using e.g. the devtools package. With a few tweaks it is also possible to install packages hosted on VSTS.

When you are on a repository in VSTS the Clone button is in the upper right corner:

clone url

With source code

If you need the source code, e.g. for contributing to the package, you might as well clone using ssh. The SSH tab has plenty of details for setting up ssh keys for authentication as well as the connection string.

Once the repository is cloned to your computer, I know of two easy ways to install the package:

  1. Open the project (i.e. the.Rproj file) in RStudio and run the Build and Install button in the Build pane.
  2. Use devtools:
setwd("path/to/repository")
devtools::install()

Without source code

If you don’t need the source code, but just want the package installed, you need the tweaks. The issue is that while devtools provides functionality for installing from a Git repository, you somehow need to authenticate when accessing VSTS.

Here I use a personal access token for authentication. If you click on Generate Git Credentials in the Clone menu you will also get an appropriate link.

This token is passed to VSTS using the git2r package with your login email as <user>:

cred <- git2r::cred_user_pass(username = "<user>", password = "<token>")

Now devtools can install the package by using the https clone URL:

devtools::install_git("<URL>", credentials = cred)

The personal access token from VSTS is only viewable once, so you have to save it somewhere if you wish to reuse it. Instead of writing it directly in cred, you can save it in an Renviron file:

vsts_pat="<token>"
It is then accesible in R as Sys.getenv("vsts_pat"). This approach has two benefits: You can easily reuse the token and you don’t enter it in the history.

Placing a .Renviron file in your home directory makes the contents available in every R session (unless a local Renviron file overrides it). Check help(Startup) or https://csgillespie.github.io/efficientR/r-startup.html for more information about Renviron.

Misc

devtools is a big package and a lightweight alternative just for installing from remote resources is remotes. However, at the time of writing remotes::install_git does not work and fails with this error:

Error in git2r::clone(x$url, bundle, progress = FALSE) :
  Error in 'git2r_clone': Unable to authenticate with supplied credentials

Digging through the source code it appears that the credentials are not passed correctly in remotes::install_git as they are in devtools::install_git.

Edit 2018-09-06: Today I saw that the issue with remotes has been resolved.

At one point I looked into incorporating this in the unofficial tfsR package. But since the bulk of the work is handled by devtools and so much input needs to be passed, I think it is overkill to wrap the two lines in a function.