Skip to content

Commit

Permalink
version 0.1-2
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrothendieck authored and gaborcsardi committed Apr 13, 2006
1 parent 0dfc862 commit 0117259
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 50 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
@@ -1,10 +1,10 @@
Package: gsubfn
Version: 0.1-1
Date: 2006-03-24
Title: gsubfn
Version: 0.1-2
Date: 2006-04-13
Title: Like gsub but with replacement function instead of string
Author: G. Grothendieck
Maintainer: Gabor Grothendieck <ggrothendieck@gmail.com>
Description: Miscellaneous string utilities
Depends: R (>= 2.0.0)
License: GPL
Packaged: Tue Apr 4 22:43:22 2006; Kates
Packaged: Mon Apr 17 14:11:49 2006; Grothendieck
43 changes: 43 additions & 0 deletions R/cati.R
@@ -0,0 +1,43 @@

# same as cat except it replaces $x (where x starts with letter and may contain
# letters, numbers and dots) or `x` (where x is any expression not containing
# backticks) with x evaluated and appends \n at end
# The end= argument can be used to append additional characters.
# e.g. cati("letters = $letters, pi = $pi\n")
cati <-
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE, env = parent.frame(),
pattern = "[$]([[:alpha:]][[:alnum:].]*)|`([^`]+)`",
backref = nchar(base::gsub("[^(]","",pattern)),
end = "")
{
# added end= argument to cat
cat.end <-
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE, end = "")
{
if (is.character(file))
if (file == "")
file <- stdout()
else if (substring(file, 1, 1) == "|") {
file <- pipe(substring(file, 2), "w")
on.exit(close(file))
}
else {
file <- file(file, ifelse(append, "a", "w"))
on.exit(close(file))
}
args <- list(...)
n <- length(args)
args[[n]] <- paste(args[[n]], end, sep = "")
.Internal(cat(args, file, sep, fill, labels, append))
}

force(env)
args <- lapply(list(...), function(x)
gsubfn(pattern, , as.character(x), env = env))
args <- c(args, file = file, sep = sep, fill = fill, labels = labels,
append = append, end = end)
do.call("cat.end", args)
}

41 changes: 0 additions & 41 deletions R/gsubfn.R
@@ -1,44 +1,3 @@
# added end= argument to cat; useful for creating wrappers such as catn below.
cat.end <-
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE, end = "")
{
if (is.character(file))
if (file == "")
file <- stdout()
else if (substring(file, 1, 1) == "|") {
file <- pipe(substring(file, 2), "w")
on.exit(close(file))
}
else {
file <- file(file, ifelse(append, "a", "w"))
on.exit(close(file))
}
args <- list(...)
n <- length(args)
args[[n]] <- paste(args[[n]], end, sep = "")
.Internal(cat(args, file, sep, fill, labels, append))
}

# same as cat except it replaces $x (where x starts with letter and may contain
# letters, numbers and dots) or `x` (where x is any expression not containing
# backticks) with x evaluated and appends \n at end
# The end= argument can be used to override appending of "\n".
# e.g. catn("letters = $letters, pi = $pi")
catn <-
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE, env = parent.frame(),
pattern = "[$]([[:alpha:]][[:alnum:].]*)|`([^`]+)`",
backref = nchar(base::gsub("[^(]","",pattern)),
end = "\n")
{
force(env)
args <- lapply(list(...), function(x)
gsubfn(pattern, , x, env = env))
args <- c(args, file = file, sep = sep, fill = fill, labels = labels,
append = append, end = end)
do.call("cat.end", args)
}

# supports function as replacement argument. Matched string is passed to
# function as arg1, with subsequent args being the backreferences.
Expand Down
16 changes: 15 additions & 1 deletion inst/NEWS
@@ -1,3 +1,17 @@

0.1-1 Added strapply function, a wrapper for gsubfn.
Changes in 0.1-2

o cati added

o updated DESCRIPTION and gsubfn-package.Rd

Changes in 0.1-1

o strapply added

o gsubfn-package.Rd added

Changes in 0.1-0

o initial release

2 changes: 1 addition & 1 deletion inst/WISHLIST
@@ -1,5 +1,5 @@

o return matches rather than do a substitution
WISHLIST

o improve layout of .Rd arg list

52 changes: 52 additions & 0 deletions man/cati.Rd
@@ -0,0 +1,52 @@
\name{cati}
\alias{cati}
\title{ cat with string interpolation}
\description{
Similar to \code{"cat"} except that the strings can include
\code{"gsubfn"}-like string interpolation.
}
\usage{
cati(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE, env = parent.frame(), pattern = "[$]([[:alpha:]][[:alnum:].]*)|`([^`]+)`", backref = nchar(base::gsub("[^(]", "", pattern)), end = "")
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{\dots}{ R objects which are coerced to character strings,
concatenated, and printed, with the remaining arguments
controlling the output.}
\item{file}{A connection, or a character string naming the file to print
to. If \code{'""'} (the default), \code{"cat"} prints to the standard
output connection, the console unless redirected by \code{"'sink'"}.}
\item{sep}{character string to insert between the objects to print.}
\item{fill}{ a logical or numeric controlling how the output is broken
into successive lines. If \code{"FALSE"} (default), only newlines
created explicitly by \code{"\\n"} are printed. Otherwise, the
output is broken into lines with print width equal to the
option 'width' if 'fill' is 'TRUE', or the value of 'fill' if
this is numeric.}
\item{labels}{ character vector of labels for the lines printed. Ignored if
'fill' is 'FALSE'.}
\item{append}{logical. Only used if the argument \code{"file"} is the
name of file
(and not a connection or \code{'"|cmd"'}). If \code{"TRUE"} output
will be
appended to \code{"file"}; otherwise, it will overwrite the contents
of \code{"file"}.}
\item{env}{Same as in \code{"gsubfn"}. Normally not used.}
\item{pattern}{Same as in \code{"gsubfn"}. Normally not used.}
\item{backref}{Same as in \code{"gsubfn"}. Normally not used.}
\item{end}{ String to be appended to the output string. \code{"sep"} is
not used to separate \code{"end"}.}
}
\details{
Similar to \code{"cat"} except that \code{"gsub"}-style string interpolcation
can be used in the arguments.
}
\value{
None (invisible 'NULL').
}
\seealso{ Also see \code{\link{gsubfn}}. }
\examples{
cati("pi = $pi, pi rounded = `round(pi,2)`\\n")
cati(1, 2, 3, sep = ",", end = "\\n")
}
\keyword{character}
12 changes: 11 additions & 1 deletion man/gsubfn-package.Rd
Expand Up @@ -18,9 +18,19 @@ based on matches rather than delimiters (using
the function \code{"'strapply'"}) and other
string transformation applications.

The
following are sources of information on \code{"gsubfn"}:
\tabular{ll}{
News\tab file.show(system.file("NEWS", package = "gsubfn"))\cr
Wish List\tab file.show(system.file("WISHLIST", package = "gsubfn"))\cr
This File\tab package?gsubfn\cr
Help files\tab ?gsubfn, ?strapply, ?cati\cr
}

Index:
\preformatted{
gsubfn Pattern Matching and Replacement
cati cat but with gsubfn-style interpolation.
strapply Apply a function over a string or strings.
}
}
Expand All @@ -31,7 +41,7 @@ strapply Apply a function over a string or strings.
gsubfn("[[:digit:]]+", function(x) as.numeric(x)+1, "(10 20)(100 30)")

# perl-style string interpolation
gsubfn(,, "pi = $pi\n")
cati("pi = $pi, e = `exp(1)`\\n")

# split out numbers
strapply("12abc34 55", "[0-9]+")
Expand Down
2 changes: 0 additions & 2 deletions man/gsubfn.Rd
@@ -1,7 +1,5 @@
\name{gsubfn}
\alias{gsubfn}
\alias{cat.end}
\alias{catn}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{ Pattern Matching and Replacement }
\description{
Expand Down

0 comments on commit 0117259

Please sign in to comment.