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 docum
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.