如何判断我的 R 脚本中没有使用哪些包? [英] How can I tell which packages I am not using in my R script?
问题描述
随着我的代码从一个版本到另一个版本,我知道有一些包我已经找到了更好/更适合手头任务的包,或者它们的目的仅限于我的一段代码现已淘汰.
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.
推荐答案
2020-04-13更新
我现在更新了引用函数以使用抽象语法树 (AST),而不是像以前那样使用正则表达式.这是解决问题的一种更强大的方法(它还没有完全确定).这可从 funchir
的 0.2.0 版中获得,现在在 CRAN 上.
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.
我刚刚开始编写一个快速而简单的函数来处理这个问题,我称之为 stale_package_check
,我已经将它添加到我的包中(funchir
).
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
).
例如,如果我们将以下脚本保存为 test.R:
e.g., if we save the following script as test.R:
library(data.table)
library(iotools)
DT = data.table(a = 1:3)
然后(从包含该脚本的目录中)运行 funchir::stale_package_check('test.R')
,我们将得到:
Then (from the directory with that script) run funchir::stale_package_check('test.R')
, we'll get:
从包data.table匹配的函数:data.table
Functions matched from package data.table: data.table
**没有从 iotools 匹配的导出函数**
**No exported functions matched from iotools**
这篇关于如何判断我的 R 脚本中没有使用哪些包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!