Writing R Extensions

Categories:

Recommended

Packages provide a mechanism for loading optional code, data and documentation as needed. The R distribution itself includes about 30 packages.

In the following, we assume that you know the library() command, including its lib.loc argument, and we also assume basic knowledge of the R CMD INSTALL utility. Otherwise, please look at R’s help pages on

?library

?INSTALL

before reading on.

For packages which contain code to be compiled, a computing environment including a number of tools is assumed; the “R Installation and Administration” manual describes what is needed for each OS.

Once a source package is created, it must be installed by the command R CMD INSTALL. See Section “Add-on-packages” in R Installation and Administration.

Other types of extensions are supported (but rare): See Section 1.10 [Package types], page 75.

Some notes on terminology complete this introduction. These will help with the reading of this manual, and also in describing concepts accurately when asking for help.

A package is a directory of files which extend R, a source package (the master files of a package), or a tarball containing the files of a source package, or an installed package, the result of running R CMD INSTALL on a source package. On some platforms (notably macOS and Windows) there are also binary packages, a zip file or tarball containing the files of an installed package which can be unpacked rather than installing from sources.

A package is not1 a library. The latter is used in two senses in R documentation.

• A directory into which packages are installed, e.g. /usr/lib/R/library: in that sense it is sometimes referred to as a library directory or library tree (since the library is a directory which contains packages as directories, which themselves contain directories).

• That used by the operating system, as a shared, dynamic or static library or (especially on Windows) a DLL, where the second L stands for ‘library’. Installed packages may contain compiled code in what is known on Unix-alikes as a shared object and on Windows as a DLL. The concept of a shared library (dynamic library on macOS) as a collection of compiled code to which a package might link is also used, especially for R itself on some platforms. On most platforms these concepts are interchangeable (shared objects and DLLs can both be loaded into the R process and be linked against), but macOS distinguishes between shared objects (extension .so) and dynamic libraries (extension .dylib).

There are a number of well-defined operations on source packages.

• The most common is installation which takes a source package and installs it in a library using R CMD INSTALL or install.packages.

• Source packages can be built. This involves taking a source directory and creating a tarball ready for distribution, including cleaning it up and creating PDF documentation from any vignettes it may contain. Source packages (and most often tarballs) can be checked, when a test installation is done and tested (including running its examples); also, the contents of the package are tested in various ways for consistency and portability.

• Compilation is not a correct term for a package. Installing a source package which contains C, C++ or Fortran code will involve compiling that code. There is also the possibility of ‘byte’ compiling the R code in a package (using the facilities of package compiler): nowadays

 

Category:

Attribution

R Core Team. Writing R Extensions. https://cran.r-project.org/doc/manuals/r-patched/R-exts.pdf

VP Flipbook Maker

Convert your work to digital flipbook with VP Online Flipbook Maker! You can also create a new one with the tool. Try it now!