• No results found

Distribute your R code with R package

N/A
N/A
Protected

Academic year: 2021

Share "Distribute your R code with R package"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

Distribute your R code with R package

Feng Li feng.li@cufe.edu.cn

School of Statistics and Mathematics Central University of Finance and Economics

June 2, 2014

(2)

Today we are going to learn...

(3)

Why package

• Packages provide a mechanism for loading optional code, data and documentation as needed.

• Code and functions are documented.

• Easy to share.

• Provide easy interface in R for code written in other languages (C, Fortran...)

(4)

The R package structure

• The sources of an R package consists of a subdirectory containing a files DESCRIPTION and NAMESPACE, and the subdirectories R, data, demo, exec, inst, man, po, src, tests, tools and vignettes (some of which can be missing, but which should not be empty).

• The package subdirectory may also contain files INDEX, configure, cleanup, LICENSE, LICENCE and NEWS.

(5)

The difference between source and binary packages

• Source package: you can check the source code. You need to compile it to make it binary.

• Binary package: Ready to use.

(6)

The DESCRIPTION file

• The Package, Version, License, Description, Title, Author, and Maintainer fields are mandatory, all other fields are optional.

(7)

The Licensing file

• It is very important that you include license information! Otherwise, it may not even be legally correct for others to distribute copies of the package, let alone use it.

(8)

Package Dependencies

• The Depends field gives a comma-separated list of package names which this package depends on.

• Those packages will be attached before the current package when library or require is called.

• Each package name may be optionally followed by a comment in parentheses specifying a version requirement. The comment should contain a comparison operator, whitespace and a valid version number, e.g. MASS (>= 3.1-20).

(9)

The INDEX file

• The optional file INDEX contains a line for each sufficiently interesting object in the package, giving its name and a description.

• Normally this file is missing and the corresponding information is automatically generated from the documentation sources when installing from source.

(10)

The R subdirectory

• The R subdirectory contains R code files, only.

• The code files to be installed must start with an ASCII (lower or upper case) letter or digit and have one of the extensions .R, .S, .q, .r, or .s.

(11)

The man subdirectory I

• The man subdirectory should contain (only) documentation files for the objects in the package in R documentation (Rd) format

• A typical R function documentation \name{load}

\alias{load}

\title{Reload Saved Datasets} \description{

Reload the datasets written to a file with the function \code{save}.

} \usage{

load(file, envir = parent.frame()) }

\arguments{

\item{file}{a connection or a character string giving the name of the file to load.}

\item{envir}{the environment where the data should be loaded.}

(12)

The man subdirectory II

} \seealso{ \code{\link{save}}. } \examples{ ## save all data

save(list = ls(), file= "all.RData")

## restore the saved values to the current environment load("all.RData")

## restore the saved values to the workspace load("all.RData", .GlobalEnv)

}

\keyword{file}

(13)

Other files

• The sources and headers for the compiled code are in src, plus optionally a file Makevars or Makefile.

• The data subdirectory is for data files.

• Data files can have one of three types as indicated by their extension: plain R code (.R or .r), tables (.tab, .txt, or .csv, see ?data for the file formats, and note that .csv is not the standard CSV format), or save() images (.RData or .rda).

• The demo subdirectory is for R scripts (for running via demo()) that demonstrate some of the functionality of the package.

(14)

Build a package

• Using R CMD check, the R package checker, one can test whether source R packages work correctly.

• Packages may be distributed in source form as âĂIJtarballsâĂİ (.tar.gz files) or in binary form.

• The source form can be installed on all platforms with suitable tools and is the usual form for Unix-like systems

• the binary form is platform-specific, and is the more common distribution form for the Windows and OS X platforms.

• Use the following commands to find more help under a termial.

• R CMD check --help

• R CMD build --help

References

Related documents

Distributed Control Design for Balancing the Grid Using Flexible Loads.. Yue Chen, Md Umar Hashmi, Joel Mathias, Ana Bušic,

Finally, the articulation of Bahrom’s hierarchies of trade in Tajikistan emphasises this family’s Uzbek background and their historical pedigree as traders in contrast to

In addition, using actor combined with writer metadata in BPR Mapping algorithm produces better recommendations than other types of metadata, and genre combined with keyword

The research questions guiding this study investigate to what extent academic achievement increased, AP participation rates increased, and AP test scores increased among

y nyhen je H3 Llapnrpana nocefiaa nOBepeHHK zra HarrJIaTH nopea Ha HMe aaapeznror pa-nror aaxrera sa 3.534 KHJIe je'fMa, 373 KHJIa 6pa- IlIHa H 339 KHJIa nIlIeHHQe H3

In this study, the strain and damage sensing possible application of carbon fiber reinforced alkali activated slag pastes has been evaluated.. Both carbon fiber dosages showed

But although The Unfortunate Traveller does contain travestied elements of the genre of apocalypse (most notably in connection with Esdras of Granada),

Most notably, defense counsel, prosecutors and judges are supportive of the additional due process protections provided when defense counsel is al- lowed in the grand jury room;