Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell what packages you have used in R

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

like image 259
aeongrail Avatar asked Feb 14 '15 07:02

aeongrail


People also ask

How do you see what packages are being used in R?

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.

Which package is used in R?

dplyr. dplyr is the package which is used for data manipulation by providing different sets of verbs like select() , arrange() , filter() , summarise() , and mutate() .

How do I know where R packages are installed?

R packages are installed in a directory called library. The R function . libPaths() can be used to get the path to the library.

How many packages do we have in R?

R is distributed with fourteen "base packages": base, compiler, datasets, grDevices, graphics, grid, methods, parallel, splines, stats, stats4, tcltk, tools, and utils.


2 Answers

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

like image 70
eh21 Avatar answered Sep 17 '22 19:09

eh21


The ‘renv’ package provides a robust solution for this nowadays via renv::dependencies.

renv::dependencies performs proper static analysis and reliably finds package dependencies even when they are declared in non-standard ways (e.g. via box::use) or via a package DESCRIPTION file rather than via library or ::.


As a quick hack I’ve previously (pre-‘renv’) used a shell script for this:

#!/usr/bin/env bash  source_files=($(git ls-files '*.R')) grep -hE '\b(require|library)\([\.a-zA-Z0-9]*\)' "${source_files[@]}" | \     sed '/^[[:space:]]*#/d' | \     sed -E 's/.*\(([\.a-zA-Z0-9]*)\).*/\1/' | \     sort -uf \     > DEPENDS 

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 of git ls-files '*.R'.

And it produces a DEPENDS file containing a very simple list of dependencies.

It only finds direct calls to library and require though – if you wrap those calls, the script won’t work.

like image 45
Konrad Rudolph Avatar answered Sep 18 '22 19:09

Konrad Rudolph