Is there an easy way to tell if an R script made use of any function in a loaded package? -
for example, if ran script.a
:
library(ggplot2) <- 12
and script.b
library(ggplot2) b <- runif(100) qplot(b)
i'd able tell script.a
did not make use of ggplot2
, whereas script.b
did.
load library , trace functions in package environment (and in namespace). i'll use small helper function this:
trap_funs <- function(env) { f <- sapply(as.list(env, all.names=true), is.function) for( n in names(f)[f] ) trace(n, bquote(stop(paste("script called function", .(n)))), where=env) }
example:
library(data.table) trap_funs(as.environment("package:data.table")) trap_funs(asnamespace("data.table"))
this second statement needed ensure calls such data.table::xxx()
trapped.
example:
> as.data.table(mtcars) tracing as.data.table(mtcars) on entry error in eval(expr, envir, enclos) : script called function as.data.table
note code interrupted.