Here's a function that completely automates package creation, compilation, and reloading. As others have noted, the utility functions package.skeleton()
and devtools::load_all()
already get you almost all the way there. This just combines their functionality, using package.skeleton()
to create the source directory in a temp directory that gets cleaned up when load_all()
is done processing it.
All you need to do is point to the source files from which you want to read in functions, and give the package a name: import()
does the rest for you.
import <- function(srcFiles, pkgName) {
require(devtools)
dd <- tempdir()
on.exit(unlink(file.path(dd, pkgName), recursive=TRUE))
package.skeleton(name=pkgName, path = dd, code_files=srcFiles)
load_all(file.path(dd, pkgName))
}
## Create a couple of example source files
cat("bar <- function() {print('Hello World')}", file="bar.R")
cat("baz <- function() {print('Goodbye, cruel world.')}", file="baz.R")
## Try it out
import(srcFiles=c("bar.R", "baz.R"), pkgName="foo")
## Check that it worked
head(search())
# [1] ".GlobalEnv" "package:foo" "package:devtools"
# [4] "package:stats" "package:graphics" "package:grDevices"
bar()
# [1] "Hello World"
foo::baz()
# [1] "Goodbye, cruel world."
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…