R custom import from namespaces

If I write an R package, I can use importFrom(plyr,colwise) to selectively import the colwise() function into my namespace. If I run the code interactively on the command line, is there a way to do the same?

One rude decision is to download the package, but not import anything, and then write a bunch of foo <- pkg::foo import assignments manually, but I don’t see how easy it is to download without import in the first place.

+4
source share
2 answers

Maybe I still need to do this (giving it real names instead of foo so that it can start)?

 loadNamespace('zoo') rollmean <- zoo::rollmean rollmean.default <- zoo::rollmean.default 

Any comments on pitfalls? I have not used loadNamespace() before.

+1
source

If you find that you want to use the same functions from a package many times, the purest solution would be to create and download a package containing only these functions.

 ## Set up package source directory dummy <- "" ## Need one object with which to initiate package skeleton package.skeleton("dummy", "dummy") ## Clean up the man subdirectory lapply(dir("dummy/man", full.names=TRUE), file.remove) ## Use NAMESPACE to pull in the functions you want funs <- c("aaply", "ddply", "adply") cat(paste0("importFrom(plyr, ", paste(funs, collapse=", "), ")"), paste0("export(", paste(funs, collapse=", "), ")"), file = "dummy/NAMESPACE", sep = "\n") ## install the package library(devtools) install("dummy") ## Confirm that it worked library(dummy) ls(2) # [1] "aaply" "adply" "ddply" environment(aaply) # <environment: namespace:plyr> aaply(matrix(1:9, ncol=3), 2, mean) # 1 2 3 # 2 5 8 
+4
source

All Articles