Is there a quick way to scan an R script and determine which packages are actually being used? By this I mean looking at all the functions called in the script and returning a list of packages containing these function names? (I know that function names are not exclusive to any one package)
Why not just take a look at packages called library() or require() ? Right. Well, I have a bad habit of downloading packages that I often use, regardless of whether I really use them in the script.
I would like to clear some scripts that I intend to use for others by removing unused packages.
I decided to change my path in 2016. Please help me get started.
Update
Some good ideas in the comments ...
# create an R file that uses a few functions fileConn<-file("test.R") writeLines(c("df <- data.frame(v1=c(1, 1, 1), v2=c(1, 2, 3))", "\n", "m <- mean(df$v2)", "\n", "describe(df) #psych package"), fileConn) close(fileConn) # getParseData approach pkg <- getParseData(parse("test.R")) pkg <- pkg[pkg$token=="SYMBOL_FUNCTION_CALL",] pkg <- pkg[!duplicated(pkg$text),] pkgname <- pkg$text pkgname # [1] "data.frame" "c" "mean" "describe"
Update 2
An ugly attempt to implement @nicola's idea:
# load all probable packages first pkgList <- list(pkgname) for (i in 1:length(pkgname)) { try(print(packageName(environment(get(pkgList[[1]][i]))))) }
This does not like the c() function, but the results look otherwise correct.
#[1] "base" #Error in packageName(environment(get(pkgList[[1]][i]))) : # 'env' must be an environment #[1] "base" #[1] "psych"