Movatterモバイル変換


[0]ホーム

URL:


Package 'pkgload'

Title:Simulate Package Installation and Attach
Description:Simulates the process of installing a package and then attaching it. This is a key part of the 'devtools' package as it allows you to rapidly iterate while developing a package.
Authors:Hadley Wickham [aut], Winston Chang [aut], Jim Hester [aut], Lionel Henry [aut, cre], Posit Software, PBC [cph, fnd], R Core team [ctb] (Some namespace and vignette code extracted from base R)
Maintainer:Lionel Henry <[email protected]>
License:MIT + file LICENSE
Version:1.4.1.9000
Built:2025-11-17 03:27:22 UTC
Source:https://github.com/r-lib/pkgload

Help Index


Run a examples for an in-development function.

Description

dev_example is a replacement forexample.run_exampleis a low-level function that takes a path to an Rd file.

Usage

dev_example(topic, quiet=FALSE)run_example(  path,  run_donttest=FALSE,  run_dontrun=FALSE,  env= new.env(parent= globalenv()),  quiet=FALSE,  macros=NULL,  run,  test)

Arguments

topic

Name or topic (or name of Rd) file to run examples for

quiet

IfTRUE, does not echo code to console.

path

Path to.Rd file

run_donttest

ifTRUE, do run⁠\donttest⁠ sections in the Rd files.

run_dontrun

ifTRUE, do run⁠\dontrun⁠ sections in the Rd files.

env

Environment in which code will be run.

macros

Custom macros to use to parse the.Rd file. See themacros argument oftools::parse_Rd(). IfNULL, then thetools::Rd2ex() (andtools::parse_Rd()) default is used.

run,test

Deprecated, seerun_dontrun andrun_donttest above.

Examples

## Not run:# Runs installed example:library("ggplot2")example("ggplot")# Runs development example:dev_example("ggplot")## End(Not run)

In-development help for package loaded with devtools

Description

dev_help() searches for source documentation provided in packagesloaded by devtools. To improve performance, the.Rd files areparsed to create to index once, then cached. Usedev_topic_index_reset() to clear that index. You can manuallyretrieve the index for a local package withdev_topic_index().

Usage

dev_help(  topic,  dev_packages=NULL,  stage="render",  type= getOption("help_type"))dev_topic_find(topic, dev_packages=NULL)dev_topic_index(path=".")dev_topic_index_reset(pkg_name)

Arguments

topic

name of help to search for.

dev_packages

A character vector of package names to search within.IfNULL, defaults to all packages loaded by devtools.

stage

at which stage ("build", "install", or "render") should⁠\\Sexpr⁠ macros be executed? This is only important if you're using⁠\\Sexpr⁠ macro's in your Rd files.

type

of html to produce:"html" or"text". Defaults toyour default documentation type.

path

Path to package.

pkg_name

Name of package.

Examples

## Not run:library("ggplot2")help("ggplot")# loads installed documentation for ggplotload_all("ggplot2")dev_help("ggplot")# loads development documentation for ggplot## End(Not run)

Drop-in replacements for help and ? functions

Description

The⁠?⁠ andhelp functions are replacements for functions of thesame name in the utils package. They are made available when a package isloaded withload_all().

Usage

# help(topic, package = NULL, ...)# ?e2# e1?e2

Arguments

topic

A name or character string specifying the help topic.

package

A name or character string specifying the package in whichto search for the help topic. If NULL, search all packages.

...

Additional arguments to pass toutils::help().

e1

First argument to pass along to⁠utils::⁠?“.

e2

Second argument to pass along to⁠utils::⁠?“.

Details

The⁠?⁠ function is a replacement forutils::?() from theutils package. It will search for help in devtools-loaded packages first,then in regular packages.

Thehelp function is a replacement forutils::help() fromthe utils package. Ifpackage is not specified, it will search forhelp in devtools-loaded packages first, then in regular packages. Ifpackage is specified, then it will search for help in devtools-loadedpackages or regular packages, as appropriate.

Examples

## Not run:# This would load devtools and look at the help for load_all, if currently# in the devtools source directory.load_all()?load_allhelp("load_all")## End(Not run)# To see the help pages for utils::help and utils::`?`:help("help","utils")help("?","utils")## Not run:# Examples demonstrating the multiple ways of supplying arguments# NB: you can't do pkg <- "ggplot2"; help("ggplot2", pkg)help(lm)help(lm, stats)help(lm,'stats')help('lm')help('lm', stats)help('lm','stats')help(package= stats)help(package='stats')topic<-"lm"help(topic)help(topic, stats)help(topic,'stats')## End(Not run)

Get the installation path of a package

Description

Given the name of a package, this returns a path to the installedcopy of the package, which can be passed to other devtools functions.

Usage

inst(name)

Arguments

name

the name of a package.

Details

It searches for the package in.libPaths(). If multipledirs are found, it will return the first one.

Examples

inst("pkgload")inst("grid")

Is the package currently under development?

Description

ReturnsTRUE orFALSE depending on if the package has been loaded bypkgload.

Usage

is_dev_package(name)

Arguments

name

the name of a package.


Load complete package

Description

load_all() loads a package. It roughly simulates what happenswhen a package is installed and loaded withlibrary(), withouthaving to first install the package. It:

  • Loads all data files in⁠data/⁠. Seeload_data() for moredetails.

  • Sources all R files in the R directory, storing results inenvironment that behaves like a regular package namespace. Seeload_code() for more details.

  • Adds a shim fromsystem.file() toshim_system.file() inthe imports environment of the package. This ensures thatsystem.file()works with both development and installed packages despite their differingdirectory structures.

  • Adds shims fromhelp() and⁠?⁠ toshim_help() andshim_question()to make it easier to preview development documentation.

  • Compiles any C, C++, or Fortran code in the⁠src/⁠ directory andconnects the generated DLL into R. Seepkgbuild::compile_dll()for more details.

  • Loads any compiled translations ininst/po.

  • Runs.onAttach(),.onLoad() and.onUnload() functions atthe correct times.

  • If you usetestthat, will load all test helpers so you canaccess them interactively. devtools sets theDEVTOOLS_LOADenvironment variable to the package name to let you check whether thehelpers are run during package loading.

is_loading() returnsTRUE when it is called whileload_all()is running. This may be useful e.g. in.onLoad hooks.A package loaded withload_all() can be identified withis_dev_package().

Usage

load_all(  path=".",  reset=TRUE,  compile=NA,  attach=TRUE,  export_all=TRUE,  export_imports= export_all,  helpers= export_all,  attach_testthat= uses_testthat(path),  quiet=NULL,  recompile=FALSE,  warn_conflicts=TRUE,  debug=TRUE)is_loading(pkg=NULL)

Arguments

path

Path to a package, or within a package.

reset

[Deprecated] This is no longer supportedbecause preserving the namespace requires unlocking its environment, whichis no longer possible in recent versions of R.

compile

IfTRUE always recompiles the package; ifNArecompiles if needed (as determined bypkgbuild::needs_compile());ifFALSE, never recompiles.

attach

Whether to attach a package environment to the searchpath. IfFALSEload_all() behaves likeloadNamespace(). IfTRUE (the default), it behaves likelibrary(). IfFALSE,theexport_all,export_imports, andhelpers arguments haveno effect.

export_all

IfTRUE (the default), export all objects.IfFALSE, export only the objects that are listed as exportsin the NAMESPACE file.

export_imports

IfTRUE (the default), export all objects that areimported by the package. IfFALSE export only objects defined in thepackage.

helpers

ifTRUE loadstestthat test helpers.

attach_testthat

IfTRUE, attachtestthat to the search path,which more closely mimics the environment within test files.

quiet

ifTRUE suppresses output from this function.

recompile

DEPRECATED. force a recompile of DLL from source code, ifpresent. This is equivalent to runningpkgbuild::clean_dll() beforeload_all()

warn_conflicts

IfTRUE, issues a warning if a function in the globalenvironment masks a function in the package. This can happen when youaccidentally source a.R file, rather than usingload_all(), or if youdefine a function directly in the R console. This is frustrating to debug,as it feels like the changes you make to the package source aren't havingthe expected effect.

debug

IfTRUE (the default), then the buildruns without optimisation (-O0) and with debug symbols (-g). Seepkgbuild::compile_dll() for details.

pkg

If supplied,is_loading() only returnsTRUE if thepackage being loaded ispkg.

Differences to regular loading

load_all() tries its best to reproduce the behaviour ofloadNamespace() andlibrary(). However it deviates from normalpackage loading in several ways.

  • load_all() doesn't install the package to a library, sosystem.file()doesn't work. pkgload fixes this for the package itself installing a shim,shim_system.file(). However, this shim is not visible to third partypackages, so they will fail if they attempt to find files within yourpackage. One potential workaround is to usefs::path_package()instead ofsystem.file(), since that understands the mechanisms thatdevtools uses to load packages.

  • load_all() loads all packages referenced inImports at load time,butloadNamespace() andlibrary() only load package dependencies asthey are needed.

  • load_all() copies all objects (not just the ones listed as exports)into the package environment. This is useful during development becauseit makes internal objects easy to access. To export only the objectslisted as exports, useexport_all = FALSE. This more closely simulatesbehavior when loading an installed package withlibrary(), and canbe useful for checking for missing exports.

Controlling the debug compiler flags

load_all() delegates topkgbuild::compile_dll() to perform the actualcompilation, during which by default some debug compiler flags areappended. If you would like to produce an optimized build instead, you canopt out by either usingdebug = FALSE, setting thepkg.build_extra_flagsoption toFALSE, or setting thePKG_BUILD_EXTRA_FLAGS environment variabletoFALSE. For further details see the Details section inpkgbuild::compile_dll().

Examples

## Not run:# Load the package in the current directoryload_all("./")# Running again loads changed filesload_all("./")# With export_all=FALSE, only objects listed as exports in NAMESPACE# are exportedload_all("./", export_all=FALSE)## End(Not run)

Load R code.

Description

Sources all.R/.r files in the⁠R/⁠ directory, storing results intothe package namespace.

Usage

load_code(path=".", quiet=NULL)

Arguments

path

Path to a package, or within a package.

quiet

ifTRUE suppresses output from this function.


Load data.

Description

Loads all.RData files in the data subdirectory.

Usage

load_data(path=".")

Arguments

path

Path to a package, or within a package.


Load a compiled DLL

Description

Load a compiled DLL

Usage

load_dll(path=".")

Arguments

path

Path to a package, or within a package.


Find file in a package.

Description

It always starts by finding by walking up the path until it finds theroot directory, i.e. a directory containingDESCRIPTION. If itcannot find the root directory, or it can't find the specified path, itwill throw an error.

Usage

package_file(..., path=".")

Arguments

...

Components of the path.

path

Place to start search for package directory.

Examples

## Not run:package_file("figures","figure_1")## End(Not run)

Helper functions for working with development packages.

Description

All functions search recursively up the directory tree from the input pathuntil they find a DESCRIPTION file.

Usage

pkg_path(path=".")pkg_name(path=".")pkg_desc(path=".")pkg_version(path=".")pkg_version_raw(path=".")pkg_ns(path=".")

Arguments

path

Path to a package, or within a package.

Functions

  • pkg_path(): Return the normalized package path.

  • pkg_name(): Return the package name.

  • pkg_desc(): Return the package DESCRIPTION as adesc::desc() object.

  • pkg_version(): Return the parsed package version.

  • pkg_version_raw(): Return the raw package version (as a string).

  • pkg_ns(): Return the package namespace.


Replacement version of system.file

Description

This function is meant to intercept calls tobase::system.file(),so that it behaves well with packages loaded by devtools. It is madeavailable when a package is loaded withload_all().

Usage

shim_system.file(..., package="base", lib.loc=NULL, mustWork=FALSE)

Arguments

...

character vectors, specifying subdirectory and file(s)within some package. The default, none, returns theroot of the package. Wildcards are not supported.

package

a character string with the name of a single package.An error occurs if more than one package name is given.

lib.loc

a character vector with path names ofR libraries.See ‘Details’ for the meaning of the default value ofNULL.

mustWork

logical. IfTRUE, an error is given if thereare no matching files.

Details

Whensystem.file is called from the R console (the globalenvironment), this function detects if the target package was loaded withload_all(), and if so, it uses a customized method of searchingfor the file. This is necessary because the directory structure of a sourcepackage is different from the directory structure of an installed package.

When a package is loaded withload_all, this function is also insertedinto the package's imports environment, so that calls tosystem.filefrom within the package namespace will use this modified version. If thisfunction were not inserted into the imports environment, then the packagewould end up callingbase::system.file instead.


Unload a package

Description

unload() attempts to cleanly unload a package, including unloadingits namespace, deleting S4 class definitions and unloading any loadedDLLs. Unfortunately S4 classes are not really designed to be cleanlyunloaded, and so we have to manually modify the class dependency graph inorder for it to work - this works on the cases for which we have testedbut there may be others. Similarly, automated DLL unloading is best testedfor simple scenarios (particularly withuseDynLib(pkgname) and mayfail in other cases. If you do encounter a failure, please file a bug reportathttps://github.com/r-lib/pkgload/issues.

unregister() is a gentler version ofunload() which removes thepackage from the search path, unregisters methods, and unregistersthe namespace. It doesn't unload the namespace or its DLL to keepit in working order in case of dangling references.

Usage

unload(package= pkg_name(), quiet=FALSE)unregister(package= pkg_name())

Arguments

package

package name.

quiet

ifTRUE suppresses output from this function.

Examples

## Not run:# Unload package that is in current directoryunload()# Unload package that is in ./ggplot2/unload(pkg_name("ggplot2/"))library(ggplot2)# unload the ggplot2 package directly by nameunload("ggplot2")## End(Not run)


[8]ページ先頭

©2009-2025 Movatter.jp