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

后端 未结 4 1300
无人及你
无人及你 2021-01-04 18: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 purp

相关标签:
4条回答
  • 2021-01-04 18:57

    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**

    0 讨论(0)
  • 2021-01-04 19:11

    Have you considered using packrat?

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

    0 讨论(0)
  • 2021-01-04 19:14

    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.

    0 讨论(0)
  • 2021-01-04 19:20

    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.

    0 讨论(0)
提交回复
热议问题