When running shiny apps in RStudio using runGitHub()
, I often found that the app would crash if required libraries couldn't be loaded. For complex apps, this was frustrating because it was difficult to know which libraries were required unless there's good documentation and/or code layout.
But this is a common problem when sharing code in general. So this little script was my own attempt at a solution.
Note: The pacman
package has a similar motivation. But as far as I can tell, it requires loading libraries with the p_load
function. So you could probably just recursively replace library
or require
calls with p_load
as an alternative to this solution.
You can run this script from RStudio or from the command line.
You can simply paste this into any script in RStudio, and hit "Run current line" to look through R code in the folder where that script is located:
## Requires two libraries, install them first if you don't have them
#install.packages("rstudioapi")
#install.packages("BiocManager")
library("rstudioapi")
library("BiocManager")
source("https://gist.githubusercontent.com/mtandon09/4a870bf4addbe46e784059bce0e5d8d6/raw/dc2927aa3e6a09b34a39f8346b5ebcfd41ce2a6d/install_R_dependencies.R")
Or explicitly:
- Place the script in the directory where you want to search for R source code (e.g. the root directory of a Shiny app)
- Open the script in RStudio
- Run the script (click 'Source' or Cmd+Shift+S)
You can call the script like this:
Rscript install_R_dependencies.R /path/to/R/code
where /path/to/R/code
is the directory you want to search in.
If that directory does not exist, or is not provided, the script will search in the folder in which it is located.
This script will find *.R
files in a given directory, parse them to find the libraries used, and try to install them from CRAN and Bioconductor.
By default, it will look for R files in the directory where this script is located.
If called from the command line with 'Rscript', you can specify a directory to look in.
I wanted this to work from RStudio (for running Shiny apps) or command line, so the function determine_self_path()
will handle that correctly. It does this by examining the output of commandArgs
. If a non-existent file path is given as a command-line argument, then the location of the script is used.
This is done in the find_R_dependencies()
function by grep
ing to find non-comment lines that contain library(
or require(
, and extracting the text right after. Note that this will obviously not work as intended in cases where the library is being loaded using a variable (i.e. the variable will be captured, not the library name), but it will not interfere with the installation function.
The other annoyance I always had with R is that you never know if a package is in CRAN or Bioconductor, so it always requires a Google-ing or trial/error. So the install_multi_source()
function will check both and install what's available.
- If a library is being loaded using a variable, it will be not be captured
- I'm not really tracking/reporting what got installed and what didn't, that would be useful
- The script will search recursively for R files, which could be a bad idea; prob should be optional/tune-able
- The libraries will be installed to the default library location
- Lol lots of other things, this code isn't really thought through or testing all that much!
I started off a long time ago with a bash one-liner that would print a string I could paste into R to run install.packages
. Here it is for kicks and giggles.
grep "library(" *.R | cut -d':' -f2 | sort | uniq | sed -e 's/^.*library(\(.*\))/\1/' | awk '{print "\"" $0 "\""}' | sort | uniq | tr '\n' ',' | sed 's/,$//' | echo "all_pkgs<-c($(cat -))"
It'll output something like this:
all_pkgs <- c("ggplot2",...
- Answer for detecting/downloading packages
- This thread about detecting script location