Reputation: 4282
For example, if ran the script.A
:
library(ggplot2)
a <- 12
and then script.B
library(ggplot2)
b <- runif(100)
qplot(b)
I'd be able to tell that script.A
did not actually make use of ggplot2
, whereas script.B
did.
Upvotes: 8
Views: 373
Reputation: 12819
Load the library normally and trace all functions in the package environment (and in the namespace). I'll use a small helper function to do 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 is needed to ensure that calls such as data.table::xxx()
also get 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 that the code was interrupted.
Upvotes: 5
Reputation: 269694
Try this:
1) First issue a library()
call for each package that you do NOT want to test for. In this case there is only one package which is the one we wish to test for so we can skip this step.
2) Run the script with library
dummied out:
library <- list
source("script.A")
rm(library) # restore
If you get no errors then the script does not depend on the package.
Upvotes: 0