The import::from
and import::into
functions provide an
alternative way to import objects (e.g. functions) from packages. It is
sometimes preferred over using library
(or require
) which will
import all objects exported by the package. The benefit over obj <-
pkg::obj
is that the imported objects will (by default) be placed in a
separate entry in the search path (which can be specified), rather in the
global/current environment. Also, it is a more succinct way of importing
several objects. Note that the two functions are symmetric, and usage is a
matter of preference and whether specifying the .into
argument is
desired. The function import::here
is short-hand for
import::from
with .into = ""
which imports into the current
environment.
from(.from, ..., .into = "imports", .library = .libPaths()[1L])here(..., .from, .library = .libPaths()[1L])
into(.into, ..., .from, .library = .libPaths()[1L])
The package from which to import, or the path to a stand-alone .R file.
Names or name-value pairs specifying objects to import. If arguments are named, then the imported object will have this new name.
The name of the search path entry. Use ""
to import
into the current environment.
character specifying the library to use. Defaults to
the latest specified library. This is not used if .from
is a
stand-alone R file.
a reference to the environment with the imports or NULL
if into = ""
, invisibly.
The function arguments can be quoted or unquoted as with e.g. library
.
In any case, the character representation is used when unquoted arguments are
provided (and not the value of objects with matching names). The period in
the argument names .into
and .from
are there to avoid name
clash with package objects. The double-colon syntax import::from
allows for imports of exported objects (and lazy data) only. To import
objects that are not exported, use triple-colon syntax, e.g.
import:::from
. The two ways of calling the import
functions
analogue the ::
and :::
operators themselves.
Note that the import
functions usually have the (intended) side-effect
of altering the search path, as they (by default) import objects into the
"imports" search path entry rather than the global environment.
The import
package is not meant to be loaded with library
(and
will output a message about this if attached), but rather it is named to make
the function calls expressive without the need to preload, i.e. it is
designed to be used explicitly with the ::
syntax, e.g.
import::from(pkg, x, y)
.
It is also possible to import objects from an R file, in which case the relative (or absolute) path to the file is provided, rather than a package name.
# NOT RUN {
import::from(parallel, makeCluster, parLapply)
import::into("imports:parallel", makeCluster, parLapply, .from = parallel)
# }
Run the code above in your browser using DataCamp Workspace