jDataLab

3 minute read

Simply, this is a self-reminder of the R commands for developing an R package by using devtools.

List all packages grouped by their directory

sapply(.libPaths(), dir)

Add rtools40 to Path in Windows

Write the line

PATH="path\to\rtools\usr\bin;${PATH}"

to .Renviron in the Documents folder.

To Create a .Rproj file

devtools::use_rstudio("path/to/dir") 

To Create a New Package

usethis::create_package()

DESCRIPTION

  • lists package needs
  • Imports: lists the packages which will be automatically installed along with your package. Comma to separate.
  • Suggests: lists the packages for running tests and vignettes

Automatically put packages in DESCRIPTION

# add to Imports
usethis::use_package(package, type="Imports", min_version=NULL)
# add to Suggests
devtools::use_package(packagae, quite=TRUE)

Use roxygen comments for each function beside its definition

  • Place a roxygen @tag (right) after #’ to supply a specific section of documentation.
  • Untagged lines will be used to generate a title, description, and details section in that order
#' Normalize one column vector to 0-1 scale
#'
#' @param v A column vector.
#' @param range the maximum value default 1
#' @return The normalized vector
#' @example normalize_column(v=c(-1, 3, 6, -2, 0))
#' @export
normalize_column <- function(v, range=1){
  v.norm = (v - min(v,na.rm=TRUE)) / (max(v,na.rm=TRUE)-min(v,na.rm=TRUE)) * range
  return(v.norm)
}

Add R Code

devtools::load_all(".")

Documenting/generating man

Add exported functions to namespace with the command:

# Ctrl/Cmd + Shift + D
devtools::document(roclets=c('rd','collate','namespace'))

Including Data in the package

There are three main ways to include data in your package, depending on what you want to do with it and who should be able to use it:

  1. If you want to store binary data and make it available to the user, put it in data/. This is the best place to put example datasets.
  2. If you want to store parsed data, but not make it available to the user, put it in R/sysdata.rda. This is the best place to put data that your functions need.
  3. If you want to store raw data, put it in inst/extdata.

Setting up:

usethis::use_data_raw()

will set up the following:

  • add ^data-raw$ to .Rbuildignore
  • write data-raw/DATASET.R You will modify data-raw/DATASET.R and finish the data preparation script in data-raw/DATASET.R. Run
usethis::use_data()

to add prepared data to package, use_data will

  • add R to Depends field in DESCRIPTION
  • save the dataset to data/dataset.rda

Bundle the Package

devtools::build()
devtools::use_build_ignore("fileordirnametobeignored")

Modify Options

If you modify global options or graphics par, save the old values and reset when you’re done:

old <- options(stringsAsFactors = FALSE)
on.exit(options(old), add = TRUE)
old <- setwd(tempdir())
on.exit(setwd(old), add = TRUE)

Display an informative message when the package loads

.onAttach <- function(libname, pkgname) {
  packageStartupMessage("Welcome to my little toolbox")
}

vignettes/

holds documents that teach your users how to solve real problems with your tools. Create a vignettes directory and a template vignette with

usethis::use_vignette("my-vignette")

which also adds template vignette as vignettes/my-vignette.Rmd.

Install the package in a local compressed file

install.packages("D:/jagertools_0.0.2.tar.gz",repos = NULL)