patternapply

Iteratively try patterns against a character vector.
git clone https://git.eamoncaddigan.net/patternapply.git
Log | Files | Refs | README | LICENSE

commit 2271dcc177da8b67a695ff4481c5b026f66a8259
parent fb136a95a87d32685ba342186c77bce2ceeddbc6
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date:   Mon, 29 Feb 2016 11:31:26 -0500

Fixed handling of defaults and added docs.

Diffstat:
MDESCRIPTION | 9+++++++--
MNAMESPACE | 3++-
MR/patternapply.R | 10+++++-----
Aman/as.data.frame.replacement_list.Rd | 19+++++++++++++++++++
Aman/as.matrix.replacement_list.Rd | 19+++++++++++++++++++
Aman/patternapply.Rd | 25+++++++++++++++++++++++++
Mtests/testthat/test_patternapply.R | 6++++++
7 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION @@ -5,7 +5,12 @@ Version: 0.1 Date: 2016-02-19 Author: Eamon Caddigan Maintainer: Eamon Caddigan <eamon.caddigan@gmail.com> -Description: Extracts information from vectors (or columns) of text data that can take one of several formats by applying regular expressions in turn until a match is found. Mostly useful for ultimately turning such a vector into a data.frame. +Description: Extracts information from vectors (or columns) of text data that + can take one of several formats by applying regular expressions in turn until + a match is found. Mostly useful for ultimately turning such a vector into a + data.frame. License: BSD 3 LazyData: TRUE -Suggests: testthat +Suggests: + testthat +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE @@ -1 +1,2 @@ -exportPattern("^[[:alpha:]]+") +# Generated by roxygen2: do not edit by hand + diff --git a/R/patternapply.R b/R/patternapply.R @@ -3,14 +3,14 @@ #' #' @param X A character vector where matches are sought. #' @param patterns A vector of regular expression patterns. -#' @param replacements A list of replacement information, must match the length -#' of \code(patterns). Each element must be a character vector. This can -#' include backreferences "\1" to "\9" to parenthesized subexpressions of the -#' corresponding pattern. +#' @param replacements A list of replacement information, which must match the +#' length of \code{patterns}. Each element must be a character vector. This can +#' include backreferences "\1" to "\9" to parenthesized subexpressions of the +#' corresponding pattern. #' #' @return A list of replacement vectors with class "replacement_list". patternapply <- function(X, patterns, - replacements = list(paste(seq_along(patterns)))) { + replacements = as.list(paste(seq_along(patterns)))) { # Check the inputs if (class(patterns) != "character") { stop("'patterns' must be a character vector") diff --git a/man/as.data.frame.replacement_list.Rd b/man/as.data.frame.replacement_list.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generics.R +\name{as.data.frame.replacement_list} +\alias{as.data.frame.replacement_list} +\title{Convert a replacement_list object into a data.frame.} +\usage{ +\method{as.data.frame}{replacement_list}(x) +} +\arguments{ +\item{x}{A replacement_list.} +} +\value{ +A data.frame. Each row corresponds to an entry in the + replacement_list. +} +\description{ +Convert a replacement_list object into a data.frame. +} + diff --git a/man/as.matrix.replacement_list.Rd b/man/as.matrix.replacement_list.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generics.R +\name{as.matrix.replacement_list} +\alias{as.matrix.replacement_list} +\title{Convert a replacement_list object into a character matrix.} +\usage{ +\method{as.matrix}{replacement_list}(x) +} +\arguments{ +\item{x}{A replacement_list.} +} +\value{ +A character matrix. Each row corresponds to an entry in the + replacement_list. +} +\description{ +Convert a replacement_list object into a character matrix. +} + diff --git a/man/patternapply.Rd b/man/patternapply.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/patternapply.R +\name{patternapply} +\alias{patternapply} +\title{Iteratively try patterns against a character vector.} +\usage{ +patternapply(X, patterns, replacements = list(paste(seq_along(patterns)))) +} +\arguments{ +\item{X}{A character vector where matches are sought.} + +\item{patterns}{A vector of regular expression patterns.} + +\item{replacements}{A list of replacement information, which must match the +length of \code{patterns}. Each element must be a character vector. This can +include backreferences "\1" to "\9" to parenthesized subexpressions of the +corresponding pattern.} +} +\value{ +A list of replacement vectors with class "replacement_list". +} +\description{ +Iteratively try patterns against a character vector. +} + diff --git a/tests/testthat/test_patternapply.R b/tests/testthat/test_patternapply.R @@ -26,3 +26,9 @@ test_that("Bad inputs raise errors", { expect_error(patternapply(letters, c("a", "b"), list("c")), "'patterns' and 'replacements' must have the same length") }) + +test_that("Default arguments work", { + expect_equal(patternapply(paste(1:20), c("^[0-9]$", "^[0-9]{2}$"))[1:20], + as.list(c(rep("1", 9), rep("2", 11)))) +}) +