Yujie Pang
Yujie Pang

Reputation: 1

How to setup a local repository for R package?

I want to setup local repository for R package, I'd like the repository works like sonatype nexus(it can proxy the central repository, and cache the artifacts after downloading the artifact from central repository).

Currently nexus does not support R repository format, so it doesn't suite what I needed to do.

Is that any existing solution for creating this repository? I don't want to create a CRAN mirror, which is too heavy for me.

Upvotes: 0

Views: 1451

Answers (1)

Ted M.
Ted M.

Reputation: 392

First, you'll want to make sure you have the following path and its directories in your system: "/R/src/contrib". If you don't have this path and these directories, you'll need to create them. All of your R packages files will be stored in the "contrib" directory.

Once you've added package files to the "contrib" directory, you can use the setRepositories function from the utils package to create the repository. I'd recommend adding the following code to your .Rprofile for a local repository:

utils::setRepositories(ind = 0, addURLs = c(WORK = "file://<your higher directories>/R"))

After editing your .Rprofile, restart R.

ind = 0 will indicate that you only want the local repository. Additional repositories can be included in the addURLs = option and are comma separated within the character vector.

Next, create the repository index with the following code:

tools::write_PACKAGES("/<your higher directories>/R/src/contrib", verbose = TRUE) 

This will generate the PACKAGE files that serve as the repository index.

To see what packages are in your repository, run the following code and take a look at the resulting data frame: my_packages <- available.packages()

At this point, you can install packages from the repo without referencing the entire path to the package installation file. For example, to install the dplyr package, you could run the following:

install.packages("dplyr", dependencies = TRUE)

If you want to take it a step further and manage a changing repository, you could install and use the miniCRAN package. Otherwise, you'll need to execute the write_PACKAGES function whenever your repository changes.

After installing the miniCRAN package, you can execute the following code to create the miniCRAN repo:

 my_packages <- available.packages()

 miniCRAN::makeRepo(
  pkgs = my_packages[,1, 
  path = "/<your higher directories>/R",
  repos = getOption("repos"), 
  type = "source",
  Rversion = R.version, 
  download = TRUE, 
  writePACKAGES = TRUE,
  quiet = FALSE
 )

You only need to execute the code above once for each repo.

Then, check to make sure each miniCRAN repo has been created. You only need to do this once for each repo:

 pkgAvail(
  repos = getOption("repos"),
  type = "source",
  Rversion = R.version,
  quiet = FALSE
 )

Whenever new package files are placed into the local repo you can update the local repo's index as follows:

miniCRAN::updateRepoIndex("/<your higher directories>/R/")

Finally, as an optional step to see if the new package is in the index, create a data frame of the available packages and search the data frame:

my_packages <- available.packages(repos = "file://<your higher directories>/R/")

This approach has worked pretty well for me, but perhaps others have comments and suggestions that could improve upon it.

Upvotes: 1

Related Questions