Skip to content

Commit

Permalink
version 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrothendieck authored and cran-robot committed Mar 16, 2018
1 parent b34bebd commit 1c22501
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 141 deletions.
26 changes: 14 additions & 12 deletions DESCRIPTION
@@ -1,11 +1,11 @@
Package: gsubfn
Version: 0.6-6
Date: 2014-08-23
Title: Utilities for strings and function arguments.
Version: 0.7
Date: 2018-03-15
Title: Utilities for Strings and Function Arguments
Author: G. Grothendieck
Maintainer: G. Grothendieck <ggrothendieck@gmail.com>
Description: gsubfn is like gsub but can take a replacement function
or certain other objects instead of the replacement string.
Description: The gsubfn function is like gsub but can take a replacement
function or certain other objects instead of the replacement string.
Matches and back references are input to the replacement function and
replaced by the function output. gsubfn can be used to split strings
based on content rather than delimiters and for quasi-perl-style string
Expand All @@ -15,13 +15,15 @@ Description: gsubfn is like gsub but can take a replacement function
lapply, optim, integrate, xyplot, Filter and any other function that
expects another function as an input argument or functions like cat
or sql calls that may involve strings where substitution is desirable.
ByteCompile: true
Depends: R (>= 2.15.0), proto
Suggests: boot, chron, doBy, grid, lattice, quantreg, reshape, svUnit,
tcltk, zoo
There is also a facility for returning multiple objects from functions
and a version of transform that allows the RHS to refer to LHS used in
the same transform.
BugReports: https://github.com/ggrothendieck/gsubfn/issues
Depends: proto
Suggests: boot, chron, grid, lattice, quantreg, svUnit, tcltk, zoo
License: GPL (>= 2)
URL: http://gsubfn.googlecode.com
Packaged: 2014-08-23 12:40:54 UTC; Louis
URL: https://github.com/ggrothendieck/gsubfn
NeedsCompilation: no
Packaged: 2018-03-15 23:17:54 UTC; Louis
Repository: CRAN
Date/Publication: 2014-08-27 21:17:14
Date/Publication: 2018-03-16 08:58:03 UTC
20 changes: 12 additions & 8 deletions MD5
@@ -1,12 +1,14 @@
46aaf69a91703493b666f212a04f2d8d *COPYING
a911eca2becf1ae734bfdf265d23a248 *DESCRIPTION
0b3cca3fa9fd32824059bf4b3d2d46ae *NAMESPACE
f5e1a6400c3a36ba690b8de009d1f001 *DESCRIPTION
a35b7b85a772fddf472d0a33202e36b6 *NAMESPACE
9080af897da275c7261481e98a8ff5d4 *R/fn.R
5ae51c26c225d561c7d10ef461a39232 *R/gsubfn.R
e211804326fafb20d618b23d485890c9 *R/list.R
a70ff11427cec20c559ecc813ed2edc9 *R/read.pattern.R
f18365c478f5e9a42454fbf9dfbbf2f3 *R/strapplyc.R
0a3f4f8e2c26d2aafc2e301b25b2bcb1 *R/transform2.R
ed4e7cca327743fdd6ae871d4f9e11d6 *R/zzz.R
d44d03bcccb0632dfe6e484309a9a225 *build/vignette.rds
e473cf190f04eb9b1623375710345daa *build/vignette.rds
fd368f5b6ac08f97596a66a7041109b4 *demo/00Index
6055e62e03c832133456d658b3b07d89 *demo/gsubfn-chron.R
d6ec8f0d982e6ac6e6d50cc164d28f7f *demo/gsubfn-cut.R
Expand All @@ -15,20 +17,22 @@ ea449b1c9a471f375a27470ff741756f *demo/gsubfn-gries.R
6c1154cb6fc406138874d72a89df60ce *demo/gsubfn-si.R
10af9aa7492b9c6beb4b939a3bc4da5d *demo/gsubfn-unitTests.R
04f8a6fcfee3a5640e02cd9a324255ed *inst/ANNOUNCE
34e30b835233d2e74281aa568a2cd427 *inst/NEWS
3ff00a9813774d75c578b20c4fd00eae *inst/NEWS
cd4675587428f7b6c4a52c1ff1303ee6 *inst/THANKS
5e676f40f47703f3849c9c54c13d7fc4 *inst/WISHLIST
a2548c174de1a204179fa1a252767ff3 *inst/doc/gsubfn.R
54fda84c246177f0ac2c26cd7ed16203 *inst/doc/gsubfn.Rnw
7e9ae0ad32d42b341a7d82f710702df3 *inst/doc/gsubfn.pdf
9d7b369e652a49602d143082b05fc184 *inst/doc/gsubfn.R
afe93556314b8464e43c7b8c09c143ba *inst/doc/gsubfn.Rnw
66bfa61dc15336c1fcee304d0722cb2c *inst/doc/gsubfn.pdf
8f197698cdb25d56eb64187b46f345c8 *inst/lipsum.txt
c9e1d04a2d77929861d6c4944abff5c3 *inst/sample.txt
f204cb6980d1189508ecd64aec6eda7f *inst/unitTests/runit.all.R
1863bf11ffc8bf378f37688240f46e18 *man/as.function.formula.Rd
7a9a388b5afa4a69bd43f326616eb7dd *man/fn.Rd
2aff224610142ce21b1bf5446cf6fcd6 *man/gsubfn-package.Rd
600f6a60c93d504653a8939e1681113c *man/gsubfn.Rd
7e5c8426be23ebf6a6622ace9cb9bbc2 *man/list.Rd
b53a82b0503f4e35bf5c3858fc4c76a1 *man/match.funfn.Rd
2b9c0620a627429a3d25ca0ee4d6fa8e *man/read.pattern.Rd
d0cf3f084f4260310a6024d0d947f00a *man/strapply.Rd
54fda84c246177f0ac2c26cd7ed16203 *vignettes/gsubfn.Rnw
997d8e08a766c663595155c223e7cb0d *man/transform2.Rd
afe93556314b8464e43c7b8c09c143ba *vignettes/gsubfn.Rnw
12 changes: 9 additions & 3 deletions NAMESPACE
@@ -1,6 +1,12 @@
# Export all names
exportPattern(".")

import(
proto
)
export("list")

S3method("[<-", "result")
S3method("as.function", "formula")
S3method("$", "fn")

import(proto)

importFrom("utils", "read.table")
12 changes: 12 additions & 0 deletions R/list.R
@@ -0,0 +1,12 @@
list <- structure(NA,class="result")
"[<-.result" <- function(x,...,value) {
args <- as.list(match.call())
args <- args[-c(1:2,length(args))]
length(value) <- length(args)
for(i in seq(along=args)) {
a <- args[[i]]
if(!missing(a)) eval.parent(substitute(a <- v,list(a=a,v=value[[i]])))
}
x
}

7 changes: 7 additions & 0 deletions R/transform2.R
@@ -0,0 +1,7 @@

transform2 <- function(`_data`, ...) {
f <- function(){}
formals(f) <- eval(substitute(as.pairlist(c(alist(...), `_data`))))
body(f) <- substitute(modifyList(`_data`, data.frame(...)))
f()
}
Binary file modified build/vignette.rds
Binary file not shown.
8 changes: 8 additions & 0 deletions inst/NEWS
@@ -1,3 +1,11 @@
Changes in 0.7-0

o transform2 added

Changes in 0.6-7

o list[...] <- added for enabling multiple return values

Changes in 0.6-6

o bug fix. strapplyc failed with "R" engine.
Expand Down
59 changes: 15 additions & 44 deletions inst/doc/gsubfn.R
Expand Up @@ -145,36 +145,7 @@ fn$lapply(list(1:4, 1:3), ~ LETTERS[x])


###################################################
### code chunk number 16: gsubfn-fn-aggregate-0
###################################################
fn$aggregate(iris[-5], iris[5], ~ mean(range(x)))

library(reshape)
fn$cast(Type ~ variable, data = melt(CO2, id = 1:3), ~~ mean(range(x)))

library(doBy) # need version 0.9 or later
fn$summaryBy(. ~ Treatment, CO2[3:5], FUN = ~~ c(midrange = mean(range(x))))


###################################################
### code chunk number 17: gsubfn-fn-aggregate
###################################################
fn$aggregate(iris[-5], iris[5], ~ mean(range(x)))


###################################################
### code chunk number 18: gsubfn-fn-aggregate-a
###################################################

library(reshape)
fn$cast(Treatment ~ variable, data = melt(CO2, id = 1:3), ~~ mean(range(x)))

library(doBy) # need version 0.9 or later
fn$summaryBy(. ~ Treatment, CO2[3:5], FUN = ~~ c(midrange = mean(range(x))))


###################################################
### code chunk number 19: gsubfn-fn-aggregate2
### code chunk number 16: gsubfn-fn-aggregate2
###################################################
set.seed(1)
X <- data.frame(X = rnorm(24), W = runif(24), A = gl(2, 1, 24), B = gl(2, 2, 24))
Expand All @@ -183,15 +154,15 @@ fn$aggregate(1:nrow(X), X[3:4], i ~ weighted.mean(X[i,1], X[i,2]))


###################################################
### code chunk number 20: gsubfn-fn-math
### code chunk number 17: gsubfn-fn-math
###################################################
fn$integrate(~1/((x+1)*sqrt(x)), lower = 0, upper = Inf)

fn$optimize(~ x^2, c(-1,1))


###################################################
### code chunk number 21: gsubfn-fn-S4
### code chunk number 18: gsubfn-fn-S4
###################################################
setClass('ooc', representation(a = 'numeric'))
fn$setGeneric('incr', x + value ~ standardGeneric('incr'))
Expand All @@ -202,7 +173,7 @@ oo


###################################################
### code chunk number 22: gsubfn-fn-quantreg-load
### code chunk number 19: gsubfn-fn-quantreg-load
###################################################
library(quantreg)
data(engel)
Expand All @@ -211,77 +182,77 @@ junk <- fn$lapply(1:9/10, tau ~ abline(coef(rq(foodexp ~ income, tau, engel))))


###################################################
### code chunk number 23: gsubfn-fn-quantreg (eval = FALSE)
### code chunk number 20: gsubfn-fn-quantreg (eval = FALSE)
###################################################
## plot(engel$income, engel$foodexp, xlab = 'income', ylab = 'food expenditure')
## junk <- fn$lapply(1:9/10, tau ~ abline(coef(rq(foodexp ~ income, tau, engel))))


###################################################
### code chunk number 24: gsubfn-fn-quantreg-repeat
### code chunk number 21: gsubfn-fn-quantreg-repeat
###################################################
plot(engel$income, engel$foodexp, xlab = 'income', ylab = 'food expenditure')
junk <- fn$lapply(1:9/10, tau ~ abline(coef(rq(foodexp ~ income, tau, engel))))


###################################################
### code chunk number 25: gsubfn-fn-zoo
### code chunk number 22: gsubfn-fn-zoo
###################################################
library(zoo)
fn$rollapply(LakeHuron, 12, ~ mean(range(x)))


###################################################
### code chunk number 26: gsubfn-fn-zoo
### code chunk number 23: gsubfn-fn-zoo
###################################################
library(boot)
set.seed(1)
fn$boot(rivers, ~ median(x[d]), R = 2000)


###################################################
### code chunk number 27: gsubfn-fn-pi (eval = FALSE)
### code chunk number 24: gsubfn-fn-pi (eval = FALSE)
###################################################
## x <- 0:50/50
## matplot(x, fn$outer(x, 1:8, ~ sin(x * k*pi)), type = 'blobcsSh')


###################################################
### code chunk number 28: gsubfn-fn-pi-repeat
### code chunk number 25: gsubfn-fn-pi-repeat
###################################################
x <- 0:50/50
matplot(x, fn$outer(x, 1:8, ~ sin(x * k*pi)), type = 'blobcsSh')


###################################################
### code chunk number 29: gsubfn-fn-matmult
### code chunk number 26: gsubfn-fn-matmult
###################################################
a <- matrix(4:1, 2); b <- matrix(1:4, 2) # test matrices
fn$apply(b, 2, x ~ fn$apply(a, 1, y ~ sum(x*y)))
a %*% b


###################################################
### code chunk number 30: gsubfn-fn-subseq
### code chunk number 27: gsubfn-fn-subseq
###################################################
L <- fn$apply(fn$sapply(1:4, ~ rbind(i,i:4), simplify = cbind), 2, ~ x[1]:x[2])
dput(L)


###################################################
### code chunk number 31: gsubfn-fn-python
### code chunk number 28: gsubfn-fn-python
###################################################
fn$sapply( 1:10, ~ if (x%%2==0) x^2, simplify = c)


###################################################
### code chunk number 32: gsubfn-fn-cat
### code chunk number 29: gsubfn-fn-cat
###################################################
fn$cat("pi = $pi, exp = `exp(1)`\n")


###################################################
### code chunk number 33: gsubfn-fn-sq
### code chunk number 30: gsubfn-fn-sq
###################################################
sq <- function(f, x) { f <- match.funfn(f); f(x^2) }

Expand Down
42 changes: 5 additions & 37 deletions inst/doc/gsubfn.Rnw
Expand Up @@ -597,45 +597,15 @@ is \code{x} here:
fn$lapply(list(1:4, 1:3), ~ LETTERS[x])
@
The next example uses \code{aggregate} to calculate the midrange
of each of \code{conc} and \code{uptake} for each \code{Type}.
The calculation is repeated using \code{cast} from the \pkg{reshape}
package and again using \code{summaryBy} from the \pkg{doBy}
package.
Note that in two of the examples there are two formulas each and
one is to be regarded as a function and the other not so we must
use double \code{~{}} to distinguish the cases.
<<gsubfn-fn-aggregate-0,echo=FALSE,results=hide>>=
fn$aggregate(iris[-5], iris[5], ~ mean(range(x)))
library(reshape)
fn$cast(Type ~ variable, data = melt(CO2, id = 1:3), ~~ mean(range(x)))
library(doBy) # need version 0.9 or later
fn$summaryBy(. ~ Treatment, CO2[3:5], FUN = ~~ c(midrange = mean(range(x))))
@
<<gsubfn-fn-aggregate>>=
fn$aggregate(iris[-5], iris[5], ~ mean(range(x)))
@
<<gsubfn-fn-aggregate-a>>=
library(reshape)
fn$cast(Treatment ~ variable, data = melt(CO2, id = 1:3), ~~ mean(range(x)))
library(doBy) # need version 0.9 or later
fn$summaryBy(. ~ Treatment, CO2[3:5], FUN = ~~ c(midrange = mean(range(x))))
@
Here is another common use of \code{aggregate} or \code{by}. This
Here is a common use of \code{aggregate} or \code{by}. This
calculates a weighted mean of the first column using weights in
the second column all grouped by columns \code{A} and \code{B}.
The \code{aggregate} example aggregates over indexes to circumvent
the restriction of a single input to the aggregation function.
Both \code{i} and \code{X} are free variables but we only
want \code{i} to be an argument so we must specify it explicitly.
\code{X} is a free variable and we only
want \code{i} to be an argument so we must specify it explicitly
(otherwise it will assume all free variables in the right hand side
are to be arguments).
<<gsubfn-fn-aggregate2>>=
set.seed(1)
X <- data.frame(X = rnorm(24), W = runif(24), A = gl(2, 1, 24), B = gl(2, 2, 24))
Expand Down Expand Up @@ -833,14 +803,12 @@ processing.
The results in this paper were obtained using \proglang{R}
\Sexpr{paste(R.Version()[6:7], collapse = ".")} with the packages
\pkg{boot} \Sexpr{gsub("-", "--", packageDescription("boot")$Version)},
\pkg{doBy} \Sexpr{gsub("-", "--", packageDescription("doBy")$Version)},
\pkg{grid} \Sexpr{gsub("-", "--", packageDescription("grid")$Version)},
\pkg{gsubfn} \Sexpr{gsub("-", "--", packageDescription("gsubfn")$Version)},
\pkg{lattice} \Sexpr{gsub("-", "--", packageDescription("lattice")$Version)},
\pkg{proto} \Sexpr{gsub("-", "--", packageDescription("proto")$Version)},
\pkg{quantreg} \Sexpr{gsub("-", "--", packageDescription("quantreg")$Version)}
and
\pkg{reshape} \Sexpr{gsub("-", "--", packageDescription("reshape")$Version)}.
\proglang{R} itself and all packages used are available from
CRAN at \url{http://CRAN.R-project.org/}.
Expand Down
Binary file modified inst/doc/gsubfn.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions man/list.Rd
@@ -0,0 +1,34 @@
\name{list}
\alias{list}
\alias{[<-.result}
\title{Multiple value assignment.}
\description{
Multiple value assignment.
}
\details{
Enables \code{list} to be used on the left hand side of assignment statements
as shown in the examples.
}
\examples{
# swap a and b without explicitly creating a temporary
a <- 1; b <- 2
list[a,b] <- list(b,a)

# get eigenvectors and eigenvalues
list[eval, evec] <- eigen(cbind(1,1:3,3:1))

# get today's month, day, year
require(chron)
list[Month, Day, Year] <- month.day.year(unclass(Sys.Date()))

# get first two components of linear model ignoring rest
list[Coef, Resid] <- lm(rnorm(10) ~ seq(10))

# assign Green and Blue (but not Red) components
list[,Green,Blue] <- col2rgb("aquamarine")

# Assign QR and QRaux but not other components
list[QR,,QRaux] <- qr(c(1,1:3,3:1))

}
\keyword{misc}

0 comments on commit 1c22501

Please sign in to comment.