How can I deal with self-written R-packages when using Renv? - r

In our team, we are sharing self-written functions (e.g. for connecting to our dbs) via a self-written package that I set up in a git-repository (non-public).
As a general rule, we avoid to install packages in our users' accounts. Instead we use renv to get reproducible environments. How can I best handle the private package with renv?
Can I somehow use renv::install() to install the package from the private git-repo with ssh-keys?
What I've done so far: I cloned the package and installed it via R CMD INSTALL path/to/folder_with_tar.gz. renv does not seem to pick up the package when running renv::init() / renv::snapshot(). I am working on using devtools to install from repository (currently waiting for IT to resolve git2r issue) but this would not solve the problem with renv.
Sorry for no MWE, I don't know how to produce one in this case.
EDIT 2022-11-08:
I want to keep this question open because this is not an answer to the question but rather a workaround. However, if using RStudio with a professional license, the best solution is to use the Package Manager. This circumvents the problem a bit: Instead of dealing with how to acces the private git from renv, the Package Manager manages the access.

Related

Using R, how to install github library without `devtools`

Let's say I wanted to install a library off of github without devtools. How would I do that?
I was helping a student in office hours today, and devtools required a lot of dependencies. It took nearly a half-an-hour just to get him ready to use it. I know that install.packages works for local files, is the github setup going to enable this?
Let's say: https://github.com/laresbernardo/lares
I understand that devtools::install_github is just a wrapper for remote::install_github which explains some of the dependency issues with these packages (and tidyverse in general - haven was broken today for Windoze).
Can I not install a package directly using install.packages from R:base using the remote source?

Install R packages outside R

So the thing is my IT department is scared of R and therefore only allows us to use it on a laptop that can't go online. Therefore they are the ones to install packages on it.
But due to covid I'm at home discovering they didn't install the requested packages. So I'm looking to see if there is a way to download these packages on a laptop with internet access, add them to an USB and then install them from the USB in R?
Quite easy, just download the package from the CRAN site e.g. https://cran.r-project.org/web/packages/imputeTS/
You can either download the "Package source" or the "Binaries" (must be your operating system.
If you are using R Studio, there is then even a menu item for installing the package - just select the file you downloaded and here you go.
If you are not using R Studio, just provide the path to your downloaded archive in the install.packages command.
The only problem is, you also need all the dependencies ...;)
So you ideally already have them and just one package is somehow missing - otherwise this can get quite time consuming to download all the dependencies - because the dependencies itself also usually have dependencies themselves...
In this case the miniCRAN solution Roland linked in the comments might be an idea. ( Offline installation of a list of packages: getting dependencies in order ) But didn't try the miniCRAN myself yet - would be interested how good this actually works.

R: force reinstallation of package's dependencies

My system installation of R is controlled by administrators, who make changes upon request from multiple users without coordination. This causes problems for me because some packages installed in my user library interact badly with upgraded packages in the system library. This breaks cron jobs and I have to spend time maintaining it.
When running install.packages or an equivalent, is there a way to make R ignore system-installed packages, and install all transitive dependencies in my user library? For example, I tried running R_LIBS=/path/to/lib R but the system install is still on .libPaths(), and dependencies of my package are still not installed in my user library.
Oh! It looks like renv is just what I want.

Installing the dependencies of a binary package automatically

In R, I have developed my own package for work (let's call it 'foo') and I have built the package and produced a binary foo.zip with the dependencies (e.g. ggplot2) listed in the description file.
When I use the R package installer in the packages tab it doesn't automatically download the dependencies from CRAN. Ultimately I don't want the end user to have to do this and I don't intend to load it to CRAN for the time being.
I have a way with: devtools::install_dep but I don't want the user to have to do this!
Use the remotes package:
remotes::install_deps("~/RStudio/foo/foo.tar.gz")
You don't say how you're expecting users to install the package. I believe if you put it on the web on a CRAN-like repository, and tell your users to install from there, R will by default install dependencies. This involves telling them how to add your repository to the repository list, so it might be just as easy to ask them to install devtools and use devtools::install_dep.
Another possibility is to distribute your package in a source version; then this answer: https://stackoverflow.com/a/38902163/2554330 gives ideas how to proceed. One that works for me to install something like "~/RStudio/foo/foo.tar.gz" is
install_url(paste0("file://", normalizePath("~/RStudio/foo/foo.tar.gz")))
If you are on Windows, you'll probably need a slightly different way to construct the URL.
Distributing binary packages is only convenient if all your users use the same version of R as you do; they aren't guaranteed to work if the minor version number changes.

R package build failing on Unix machines due to missing GSL - GNU Scientific Library

I am facing a particularly vexing problem with R package development. My own package, called ggstatsplot (https://github.com/IndrajeetPatil/ggstatsplot), depends on userfriendlyscience, which depends on another package called MBESS, which itself ultimately depends on another package called gsl. There is no problem at all for installation of ggstatsplot on a Windows machine (as assessed by AppVeyor continuous integration platform: https://ci.appveyor.com/project/IndrajeetPatil/ggstatsplot).
But whenever the package is to be installed on Unix machines, it throws the error that ggstatsplot can't be downloaded because userfriendlyscience and MBESS can't be downloaded because gsl can't be downloaded. The same thing is also revealed on Travis continuous integration platform with virtual Unix machines, where the package build fails (https://travis-ci.org/IndrajeetPatil/ggstatsplot).
Now one way to solve this problem for the user on the Unix machine is to configure GSL (as described here:
installing R gsl package on Mac), but I can't possibly expect every user of ggstatsplot to go through the arduous process of configuring GSL. I want them to just run install.packages("ggstatsplot") and be done with it.
So I would really appreciate if anyone can offer me any helpful advice as to how I can make my package user's life simpler by removing this problem at its source. Is there something I should include in the package itself that will take care of this on behalf of the user?
This may not have a satisfying solution via changes to your R package (I'm not sure either way). If the gsl package authors (which include a former R Core member) didn't configure it to avoid a pre-req installation of a linux package, there's probably a good reason not to.
But it may be some consolation that most R+Linux users understand that some R packages first require installing the underlying Linux libraries (eg, through apt or dnf/yum).
Primary Issue: making it easy for the users to install
Try to be super clear on the GitHub readme and the CRAN INSTALL file. The gsl package has decent CRAN directions. This leads to the following bash code:
sudo apt-get install libgsl0-dev
The best example of clear (linux pre-req package) documentation I've seen is from the curl and sf packages. sf's CRAN page lists only the human names of the 3 libraries, but the GitHub page provides the exact bash commands for three major distribution branches. The curl package does this very well too (eg, CRAN and GitHub). For example, it provides the following explanation and bash code:
Installation from source on Linux requires libcurl. On Debian or Ubuntu use libcurl4-openssl-dev:
sudo apt-get install -y libcurl-dev
Ideally your documentation would describe how do install the gsl linux package on multiple distributions.
Disclaimer: I've never developed a package that directly requires a Linux package, but I use them a lot. In case more examples would help, this doc includes a script I use to install stuff on new Ubuntu machines. Some commands were stated explicitly in the package documentation; some had little or no documentation, and required research.
edit 2018-04-07:
I encountered my new favorite example: the sys package uses a config file to produce the following message in the R console. While installing 100+ packages on a new computer, it was nice to see this direct message, and not have to track down the R package and the documentation about its dependencies.
On Debian/Ubuntu this package requires AppArmor.
Please run: sudo apt-get install libapparmor-dev
Another good one is pdftools, that also uses a config file (and is also developed by Jeroen Ooms).
Secondary Issue: installing on Travis
The userfriendly travis config file apparently installs a lot of binaries directly (including gsl), unlike the current ggstatsplot version.
Alternatively, I'm more familiar with telling travis to install the linux package, as demonstrated by curl's config file. As a bonus, this probably more closely replicates what typical users do on their own machines.
addons:
apt:
packages:
- libcurl4-openssl-dev
Follow up 2018-03-13 Indrajeet and I tweaked the travis file so it's working. Two sections were changed in the yaml file:
The libgsl0-dev entry was added under the packages section (similar to the libcurl4-openssl-dev entry above).
Packages were listed in the r_binary_packages section so they install as binaries. The build was timing out after 50 minutes, and now it's under 10 min. In this particular package, the r_binary_packages section was nested in the Linux part of the Travis matrix so it wouldn't interfere with his two OS X jobs on Travis.

Resources