18

As my code evolves from version to version, I'm aware that there are some packages for which I've found better/more appropriate packages for the task at hand or whose purpose was limited to a section of code which I've now phased out.

Is there any easy way to tell which of the loaded packages are actually used in a given script? My header is beginning to get cluttered.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • I think this question is fundamentally unanswerable. For example, two packages might export the same function and 99% of the time work identically. For example, `data.table::between(bit64::as.integer64(235732032595), 0, 1)` vs `dplyr::between(bit64::as.integer64(235732032595), 0, 1)`. – Hugh Jul 18 '18 at 02:53
  • @Hugh of course, the R interpreter can tell the difference, so in principle, a parsing tool should too. for my purposes I think of that more as an edge case (it's usually obvious if I'm using data.table or dplyr in a script) as the stale packages tend to be more obscure/tailor-made to run one regression, etc. – MichaelChirico Jul 18 '18 at 05:25

4 Answers4

16

Update 2020-04-13

I've now updated the referenced function to use the abstract syntax tree (AST) instead of using regular expressions as before. This is a much more robust way of approaching the problem (it's still not completely ironclad). This is available from version 0.2.0 of funchir, now on CRAN.


I've just got around to writing a quick-and-dirty function to handle this which I call stale_package_check, and I've added it to my package (funchir).

e.g., if we save the following script as test.R:

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

Then (from the directory with that script) run funchir::stale_package_check('test.R'), we'll get:

Functions matched from package data.table: data.table

**No exported functions matched from iotools**

Community
  • 1
  • 1
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
7

Have you considered using packrat?

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

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
celineu
  • 576
  • 1
  • 5
  • 18
  • seems a bit counterintuitive to install a package to help with this problem! i'm having troubles running `packrat::init()` because I just reinstalled R--I think it's looking for packages I haven't reinstalled yet. This seems like the most tailor-made solution, will accept when I figure out my problems locally – MichaelChirico Apr 03 '15 at 18:45
  • 1
    Am I correct that `packrat::init()` looks in the current working directory for _all_ `R` files in the current working directory. This isn't exactly what I wanted... close, but not quite. – MichaelChirico Jun 03 '15 at 23:48
2

I've written a command-line script to accomplish this task. You can find it in this Github gist. I'm sure there are edge cases that it misses, but it works pretty well, on both R scripts and Rmd files.

Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
0

My approach always is to close my R script or IDE (i.e. RStudio) and then start it again. After this I run my function without loading any dependecies/packages beforehand. This should result in various warning and error messages telling you which functions couldn't be found and executed. This again will give you hints on what packages are necessary to load beforehand and which one you can leave out.

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • http://stackoverflow.com/questions/7505547/detach-all-packages-while-working-in-r this might avoid restarts of R – rmuc8 Apr 02 '15 at 15:01