How to tell what packages you have used in R

2019-04-04 17:46发布

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

5条回答
神经病院院长
2楼-- · 2019-04-04 18:00

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

查看更多
小情绪 Triste *
3楼-- · 2019-04-04 18:09

I am not sure of a good way to automatize this... but what you could do is:

  1. Open a new R console
  2. 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, and base.

    You can unload any extra libraries using:

    detach("package:<packageName>", unload=TRUE)
    
  3. Now run the script after commenting all of the library and require calls and see which functions give an error.

  4. To get which package is required by each function type in the console:

    ??<functionName>
    
  5. Load the required packages and re-run steps 3-5 until satisfied.

查看更多
三岁会撩人
4楼-- · 2019-04-04 18:11

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.

查看更多
贪生不怕死
5楼-- · 2019-04-04 18:21

I’ve previously 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.

查看更多
劳资没心,怎么记你
6楼-- · 2019-04-04 18:27

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 found XRtools::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.

enter image description here

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

查看更多
登录 后发表回答