Search code examples
rbitbucketdevtools

Private Bitbucket package dependency in R package


Summary

When creating a package, I can list CRAN dependencies in the Depends list in the DESCRIPTION file. This documentation outlines how to list Bitbucket dependencies, eg. Remotes: bitbucket::sulab/mygene.r@default.

However, I don't know how to do this when authentication is needed to access the repository.

Attempt

I've tried putting the following code into the main packagename.R file. The function contents work fine as a snippet at the top of a standalone script:

.onLoad <- function(libname, pkgname) {
  otherPackageVersion <- "1.0"
  if (suppressWarnings(suppressPackageStartupMessages(require("otherPackageName", quietly = TRUE, character.only = TRUE)))) {
    if (installed.packages()[installed.packages()[,"Package"] == "otherPackageName", "Version"] != otherPackageVersion) {
      remove.packages("otherPackage")
      devtools::install_bitbucket(sprintf("bitbucketUser/otherPackageName@%s", otherPackageVersion), auth_token = Sys.getenv("BITBUCKET_PAT"))
    }
  } else {
    devtools::install_bitbucket(sprintf("bitbucketUser/otherPackageName@%s", otherPackageVersion), auth_token = Sys.getenv("BITBUCKET_PAT"))
  }
}

but R CMD check fails saying it cannot be loaded after hanging for a while:

checking whether package ‘packageName’ can be installed ... ERROR
Installation failed.

Further Detail

The version of devtools I have loaded is 1.12.0.9000 (see this Github thread) which I installed using devtools::install_github("hadley/devtools#1220"). This allows me to install private Bitbucket R packages using an App Password stored in an environment variable, rather than committing my username/password in plaintext.


Solution

  • This will not be possible until this (a pull request using Bitbucket PATs) is merged into the devtools package.

    EDIT: Checking in on this many years later, it's sorted for me by the current version of devtools (2.4.3) using a BitBucket App Password with Repo Read/Write and Project Read/Write permissions.