source()
all of the source files and data files in a package
into an environment on the search path. If an environment is not
already attached by a previous invocation of source.pkg()
, one
will be created and attached. It also loads DLLs if requested, and if
those DLLs can be found. This function is intended for use in code
development, in situations where code from a package can be tested
without worrying about imports, namespaces, etc.
source.pkg(pkg.dir = getOption("scriptests.pkg.dir", "pkg"), pattern = ".*", suffix = "\\.R$", dlls = c("no", "check", "build", "src"), unload=FALSE, pos = NA, all = FALSE, reset.function.envirs=TRUE, path=getOption("scriptests.pkg.path", default=getwd()), get.sccv=getOption('source.pkg.sccversion'))
/DESCRIPTION
.pattern
and suffix
. no
: don't load any DLLs
check
: look in
.Rcheck//libs
and .Rcheck//libs
build
: look in the directory build
(can be
created with R CMD INSTALL -l build
, and
before loading each time, it may be necessary to do
dyn.unload(...)
, using the path of the DLL as printed.)
TRUE
, all files matching the pattern and suffix
are sourced, if FALSE
, only files that have changed since last
sourced are sourced again. TRUE
the environments on all
functions sourced are set to the global environment. See NOTE below
for explanation.path
is the
current directory. By default, R source files are looked for in the
directory found by appending pkg.dir
to path
, i.e.
//R/*.R
. If path
contains the string
$PKG
, pkg.dir
is substituted for $PKG
instead
of being appended to path
. path
is remembered and the
same value used as the default in subsequent invocations.
svnversion
. source.pkg()
returns as its value a list of the
problems encountered when sourcing the files.scriptests.pkg.dir
is set in the global
environment to the directory of the package whose code was sourced. The package directory (pkg.dir
) does not need to be the same as
the package name. The package name (refered to here as
pkg.name
) is found by reading the DESCRIPTION
file.
All the objects created by the .R
files are created in a
special environment named pkgcode:
. If this environment
does not already exist, it will be created and attached to the search
path in position pos
. If it does exist, no attempt is made to
clean it before sourcing the .R
files. All functions and
objects defined in the .R
files will be visible -- namespaces
are not implemented.
The easiest way to use this function is when the working directory of
the R session is the directory where each package lives, i.e., the R
code for package mypackage
will be in the directory
./mypackage/R
(relative to the working directory of the R
session.) However, if the package directory is located elsewhere,
supply it as path=
, and this will be remembered for future
invocations of source.pkg()
.
This function does not attempt to replicate all the actions involved in creating and attaching a package. It does the following:
pkgcode:
where
is the name of the package (if it doesn't already exist)
Depends
line in the DESCRIPTION
file
and loads specified packages
.R
files in the R
subdirectory of the package, and, as appropriate, the R/windows
or R/unix
subdirectories, and uses sys.source()
to
read these into the pkgcode:
environment. If there is a
Collate
field in the DESCRIPTION
files, this is used to
sort the files before sourcing them.
.Rdata
and .rda
files in the
data
subdirectory, and uses load()
to
read these into the pkgcode:
environment
dlls=="check"
(not the default), source.pkg()
looks for DLLs (SO files under Unix) in the directory left by R
CMD check
, i.e., in .Rcheck//libs
or .Rcheck//libs
, and uses
dyn.load()
to load these DLLs. If the DLL was already loaded
(as indicated by getLoadedDLLs()
), dyn.unload()
is
called first. Be aware that unloading and reloading a DLL is not a
reliable operation under many OS's, and even when the call completes
without apparent error, the R session can be corrupted.
http://n4.nabble.com/Lightweight-package-idea-td924000.html#a924000
(link no longer active)
http://gist.github.com/180883
, reads DESCRIPTION, loads dependencies, respects
collation order (link no longer active)
runtests()
shares the options()
variables
scriptests.pkg.dir
and scriptests.pkg.path
that provide
defaults for the pkg.dir
and path
arguments.scriptests-package gives and overview of the package.
## Not run:
# # sourcing the code in a package stored in <mypackage>/{DESCRIPTION,R,man,tests}:
# source.pkg("<mypackage>")
# # sourcing the code in a package stored in path/to/dir/<mypackage>/{DESCRIPTION,R,man,tests}:
# source.pkg("<mypackage>", path="path/to/dir")
# # sourcing the code in a package stored in pkg/{DESCRIPTION,R,man,tests}:
# # where "pkg" is unrelated to the name of the pacakage
# source.pkg("pkg", path="path/to/dir")
# # sourcing the code in a package stored in <mypackage>/pkg/{DESCRIPTION,R,man,tests}:
# source.pkg("<mypackage>", path="$PKG/pkg")
# ## End(Not run)
Run the code above in your browser using DataCamp Workspace