Getting started with R LanguageData framesReading and writing tabular data in plain-text files (CSV, TSV, etc.)Pipe operators (%>% and others)Linear Models (Regression)data.tableboxplotFormulaSplit functionCreating vectorsFactorsPattern Matching and ReplacementRun-length encodingDate and TimeSpeeding up tough-to-vectorize codeggplot2ListsIntroduction to Geographical MapsBase PlottingSet operationstidyverseRcppRandom Numbers GeneratorString manipulation with stringi packageParallel processingSubsettingDebuggingInstalling packagesArima ModelsDistribution FunctionsShinyspatial analysissqldfCode profilingControl flow structuresColumn wise operationJSONRODBClubridateTime Series and Forecastingstrsplit functionWeb scraping and parsingGeneralized linear modelsReshaping data between long and wide formsRMarkdown and knitr presentationScope of variablesPerforming a Permutation TestxgboostR code vectorization best practicesMissing valuesHierarchical Linear ModelingClassesIntrospection*apply family of functions (functionals)Text miningANOVARaster and Image AnalysisSurvival analysisFault-tolerant/resilient codeReproducible RUpdating R and the package libraryFourier Series and Transformations.RprofiledplyrcaretExtracting and Listing Files in Compressed ArchivesProbability Distributions with RR in LaTeX with knitrWeb Crawling in RArithmetic OperatorsCreating reports with RMarkdownGPU-accelerated computingheatmap and heatmap.2Network analysis with the igraph packageFunctional programmingGet user inputroxygen2HashmapsSpark API (SparkR)Meta: Documentation GuidelinesI/O for foreign tables (Excel, SAS, SPSS, Stata)I/O for database tablesI/O for geographic data (shapefiles, etc.)I/O for raster imagesI/O for R's binary formatReading and writing stringsInput and outputRecyclingExpression: parse + evalRegular Expressions (regex)CombinatoricsPivot and unpivot with data.tableInspecting packagesSolving ODEs in RFeature Selection in R -- Removing Extraneous FeaturesBibliography in RMDWriting functions in RColor schemes for graphicsHierarchical clustering with hclustRandom Forest AlgorithmBar ChartCleaning dataRESTful R ServicesMachine learningVariablesThe Date classThe logical classThe character classNumeric classes and storage modesMatricesDate-time classes (POSIXct and POSIXlt)Using texreg to export models in a paper-ready wayPublishingImplement State Machine Pattern using S4 ClassReshape using tidyrModifying strings by substitutionNon-standard evaluation and standard evaluationRandomizationObject-Oriented Programming in RRegular Expression Syntax in RCoercionStandardize analyses by writing standalone R scriptsAnalyze tweets with RNatural language processingUsing pipe assignment in your own package %<>%: How to ?R Markdown Notebooks (from RStudio)Updating R versionAggregating data framesData acquisitionR memento by examplesCreating packages with devtools

Creating packages with devtools

Other topics

Creating and distributing packages

This is a compact guide about how to quickly create an R package from your code. Exhaustive documentations will be linked when available and should be read if you want a deeper knowledge of the situation. See Remarks for more resources.

The directory where your code stands will be refered as ./, and all the commands are meant to be executed from a R prompt in this folder.


Creation of the documentation

The documentation for your code has to be in a format which is very similar to LaTeX.

However, we will use a tool named roxygen in order to simplify the process:

install.packages("devtools")
library("devtools")
install.packages("roxygen2")
library("roxygen2")

The full man page for roxygen is available here. It is very similar to doxygen.

Here is a practical sample about how to document a function with roxygen:

#' Increment a variable.
#'
#' Note that the behavior of this function
#' is undefined if `x` is not of class `numeric`.
#'
#' @export
#' @author  another guy
#' @name    Increment Function
#' @title   increment
#'
#' @param x   Variable to increment
#' @return    `x` incremented of 1
#'
#' @seealso   `other_function`
#'
#' @examples
#' increment(3)
#' > 4
increment <- function(x) {
  return (x+1)
}

And here will be the result.

It is also recommanded to create a vignette (see the topic Creating vignettes), which is a full guide about your package.


Construction of the package skeleton

Assuming that your code is written for instance in files ./script1.R and ./script2.R, launch the following command in order to create the file tree of your package:

package.skeleton(name="MyPackage", code_files=c("script1.R","script2.R"))

Then delete all the files in ./MyPackage/man/. You have now to compile the documentation:

roxygenize("MyPackage")

You should also generate a reference manual from your documentation using R CMD Rd2pdf MyPackage from a command prompt started in ./.


Edition of the package properties

1. Package description

Modify ./MyPackage/DESCRIPTION according to your needs. The fields Package, Version, License, Description, Title, Author and Maintainer are mandatory, the other are optional.

If your package depends on others packages, specify them in a field named Depends (R version < 3.2.0) or Imports (R version > 3.2.0).

2. Optional folders

Once you launched the skeleton build, ./MyPackage/ only had R/ and man/ subfolders. However, it can have some others:

  • data/: here you can place the data that your library needs and that isn't code. It must be saved as dataset with the .RData extension, and you can load it at runtime with data() and load()
  • tests/: all the code files in this folder will be ran at install time. If there is any error, the installation will fail.
  • src/: for C/C++/Fortran source files you need (using Rcpp...).
  • exec/: for other executables.
  • misc/: for barely everything else.

Finalization and build

You can delete ./MyPackage/Read-and-delete-me.

As it is now, your package is ready to be installed.

You can install it with devtools::install("MyPackage").

To build your package as a source tarball, you need to execute the following command, from a command prompt in ./ : R CMD build MyPackage


Distribution of your package

Through Github

Simply create a new repository called MyPackage and upload everything in MyPackage/ to the master branch. Here is an example.

Then anyone can install your package from github with devtools:

install_package("MyPackage", "your_github_usename")

Through CRAN

Your package needs to comply to the CRAN Repository Policy. Including but not limited to: your package must be cross-platforms (except some very special cases), it should pass the R CMD check test.

Here is the submission form. You must upload the source tarball.

Creating vignettes

A vignette is a long-form guide to your package. Function documentation is great if you know the name of the function you need, but it’s useless otherwise. A vignette is like a book chapter or an academic paper: it can describe the problem that your package is designed to solve, and then show the reader how to solve it.

Vignettes will be created entirely in markdown.

Requirements

  • Rmarkdown: install.packages("rmarkdown")
  • Pandoc

Vignette creation

devtools::use_vignette("MyVignette", "MyPackage")

You can now edit your vignette at ./vignettes/MyVignette.Rmd.

The text in your vignette is formatted as Markdown.

The only addition to the original Markdown, is a tag that takes R code, runs it, captures the output, and translates it into formatted Markdown:

```{r}
# Add two numbers together
add <- function(a, b) a + b
add(10, 20)
```

Will display as:

# Add two numbers together
add <- function(a, b) a + b
add(10, 20)
## [1] 30

Thus, all the packages you will use in your vignettes must be listed as dependencies in ./DESCRIPTION.

Contributors

Topic Id: 10884

Example Ids: 32602,32610

This site is not affiliated with any of the contributors.