How can I determine which packages I am not using in my R script?

As my code evolves from version to version, I know that there are some packages for which I have found better / more suitable packages for the task at hand or whose purpose was limited to a section of code, now ceases to work.

Is there any easy way to determine which of the downloaded packages are actually used in a given script? My headline is starting to clog.

+3
source share
3 answers

I just started writing a quick-dirty function to handle this, which I call stale_package_check , and I added it to my package ( funchir ); this feature is not yet included in the CRAN version.

It performs a quick (not perfect) check for all library(*) instances in the script, and then searches for a regular expression for all exported packages in the supplied file.

for example, if we save the following script as test.R:

 library(data.table) library(iotools) DT = data.table(a = 1:3) 

Then (from anywhere) run funchir::stale_package_check('test.R') , we get:

Functions mapped to package data .table: data.table

** No exported features associated with iotools **

It is still in beta (for example, I did not find the time to match the calls to require , currently only calls to the library ), but it is still useful.

0
source

Do you consider using packrat ?

packrat::clean() will remove unused packages, for example.

+4
source

My approach always is to close my R script or IDE (i.e. RStudio) and then run it again. After that, I run my function without first downloading any dependencies / packages. This should result in various warnings and error messages telling you which functions cannot be found and performed. This again will give you tips on which packages you need to download in advance, and which one you can leave.

0
source

Source: https://habr.com/ru/post/1237946/


All Articles