Publish an R Package to a private repository and use

Overview
Let’s say we want to manage an R code base as a package. The benefits might include:
- a good way to distribute R code for other people
- a good way to document R code
- it keeps track R functions for reuse
What if we don’t want to share the package on the public internet (for corporate reasons), and have an R Studio server (installed on Linux) as our infrastructure.
R Studio Server
For demo purposes I have installed R Studio server on my Chromebook using the Ubuntu/Debian instructions Download RStudio Server. If you have a Windows machine this will also work Using RStudio Server in Windows WSL2
Now let’s check that the service is running.
service rstudio-server status
Since the service is running on our local machine we can access it on port 8787 like this: http://127.0.0.1:8787
Example
For this example we are going to create the most basic package we can think of. A package that contains a single function that prints out hello world
. Let’s call the package helloworldr.
We want a documentation site as well.
Initial install
We will use the GUI in R Studio Server to start the template for our Hello World R package.
Select the parent directory (the New Project Wizard will create a directory with the name of the project).
In the directory it creates you will see files like this:
Now we need package down for the documentation.
usethis::use_pkgdown()
Of course Yes is the wrong answer to 1, 2 or 3.
If you have installed Node.js modules in the past, or have seen memes about installing Node.js modules, you haven’t seen nothing yet, this will take a long time.
R Environment
Since we are using renv to manage our package dependencies and versions, and we have installed a lot of package dependencies through using packagedown, we need to update the lock file.
renv::snapshot()
You should see the JSON in your renv.lock
file now contains a list of all the package dependencies (if you checked it before hand you would have seen not much at all).
So this is how we got this far. But now, to follow the rest we are going to continue with something I have prepared earlier.
Hello World R
After following these steps, and making a few modifications to the function, we end up with this:
https://github.com/mortie23/helloworldr
If you were following, from here it might be easier to delete what you have and clone this one. Otherwise, the differences you will need to at least continue functionally are just the build-deploy.R
script.
Deploy to private repository
Before we build and deploy the package to a private repository, we need to know what a “private repository” is and how to create one.
All it is a directory on a file system! So it can be created like this:
# Create complete path and parents as needed
mkdir -p /opt/privatePkgs/src/contrib/
Now just source the build-deploy.R
script.
Check the package was built and deployed:
cd /opt/privatePkgs/src/contrib/
ll
Yep, there is the tar gzipped up pacakge and the PACKAGE repo metadata.
Permissions Size User Date Modified Name
.rwxrwx--- 1.2k mortimer 30 Nov 10:42 helloworldr_0.0.0.9000.tar.gz
.rw-r--r-- 210 mortimer 30 Nov 10:42 PACKAGES
.rw-r--r-- 157 mortimer 30 Nov 10:42 PACKAGES.gz
.rw-r--r-- 384 mortimer 30 Nov 10:42 PACKAGES.rds
Build the docs page
pkgdown::build_site()
Preview the site built in the docs
directory.
# Sorry, I don't know how to run a basic HTTP server in R, let's use Python
cd docs
python -m http.server
Hello World R function use
So we have a package deployed to a private repository, we can now use the functions in other downstream projects.
Start by cloning this Git repository down to your machine.
https://github.com/mortie23/helloworldr-fun
In the renv.lock
file note the refeerence to the Repository:
{
"Name": "private",
"URL": "file:///opt/privatePkgs"
}
and the reference to our package:
{
"Package": "helloworldr",
"Version": "0.0.0.9000",
"Source": "Repository"
}
If you have all the previous section complete correctly, you should be able to run:
renv::restore()
and our helloworldr package should be installed into the new R projects renv and the function should now be available.
Conclusion
Well, that took longer than I thought!