I have a very long R script with many if statements and exception cases. As i've been going, if been importing and testing libraries as I've gone and haven't really documented them very well. The problem is that if I run this from a clean installation, i'm not sure which statements the script will run, and so which libraries will be needed.
My question is: Is there any R function to test which libraries are being used in a script?
EDIT: I have not used all of the libraries that have been installed so print(sessionInfo())
won't be useful but and I just want to start the script with an install.packages
function
I found the
list.functions.in.file()
function from NCmisc (install.packages("NCmisc")
) quite helpful for this:list.functions.in.file(filename, alphabetic = TRUE)
For more info see this link: https://rdrr.io/cran/NCmisc/man/list.functions.in.file.html
I am not sure of a good way to automatize this... but what you could do is:
Check with
sessionInfo
that you don't have extra packages loaded.You could check this using
sessionInfo
. If you, by default, load extra packages (e.g. using your .RProfile file) I suggest you avoid doing that, as it's a recipe for disaster.Normally you should only have the base packages loaded:
stats
,graphics
,grDevices
,utils
,datasets
,methods
, andbase
.You can unload any extra libraries using:
Now run the script after commenting all of the
library
andrequire
calls and see which functions give an error.To get which package is required by each function type in the console:
Load the required packages and re-run steps 3-5 until satisfied.
You might want to look at the checkpoint function from Revolution Analytics on GitHub here: https://github.com/RevolutionAnalytics/checkpoint
It does some of this, and solves the problem of reproducibility. But I don't see that it can report a list of what you are using.
However if you looked a the code you probably get some ideas.
I’ve previously used a shell script for this:
This uses Git to collect all R files under version control in a project. Since you should be using version control anyway this is normally a good solution (although you may want to adapt the version control system). For the few cases where the project isn’t under version control you should (1) put it under version control. Or, failing that, (2) use
find . -regex '.*\.[rR]'
instead ofgit ls-files '*.R'
.And it produces a
DEPENDS
file containing a very simple list of dependencies.It only finds direct calls to
library
andrequire
though – if you wrap those calls, the script won’t work.I had a similar need when I needed to convert my code into a package, thus I need to identify every package dependency and either import or use full qualified name.
In reading book
Extending R
I foundXRtools::makeImports
can scan a package and find all packages need to be imported. This doesn't solve our problem yet as it only apply to existing package, but it provided the main insight on how to do it.I made a function and put it into my package
mischelper
. You can install the package, either use the RStudio addin menu to scan current file or selected code, or use command line functions. Every external function (fun_inside) and the function that called it (usage) will be listed in table.You can now go to each function, press F1 to find which package it belongs. I actually have another package that can scan all installed packages for function names and build a database, but that may cause more false positives for this usage because if you only loaded some packages, pressing F1 only search loaded packages.
See details of the usage in my package page
https://github.com/dracodoc/mischelper