Skip to content

Commit

Permalink
version 0.2-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrothendieck authored and gaborcsardi committed Oct 8, 2006
1 parent d13de92 commit 29fef4f
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 93 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
@@ -1,6 +1,6 @@
Package: gsubfn
Version: 0.2-0
Date: 2006-10-05
Version: 0.2-1
Date: 2006-10-08
Title: Miscellaneous utilities for strings and function arguments.
Author: G. Grothendieck
Maintainer: G. Grothendieck <ggrothendieck@gmail.com>
Expand All @@ -16,4 +16,4 @@ Description: gsubfn is like gsub but can take a replacement function
Depends: R (>= 2.0.0)
License: GPL (Version 2 or later)
URL: http://code.google.com/p/gsubfn/
Packaged: Thu Oct 5 20:22:59 2006; Kates
Packaged: Mon Oct 9 23:13:03 2006; Kates
50 changes: 37 additions & 13 deletions R/fn.R
@@ -1,6 +1,6 @@

as.function.formula <- function(x, ...) {
vars <- all.vars(x[[2]])
vars <- setdiff(all.vars(x[[2]]), c("letters", "LETTERS", "pi"))
if (length(vars) == 0) {
f <- function() {}
} else {
Expand Down Expand Up @@ -29,26 +29,48 @@ fn <- structure(NA, class = "fn")
mc1 <- mc[-1]
nm <- names(mc1)
if (is.null(nm)) nm <- rep("", length(args))
idx <- match("simplify", tolower(nm), nomatch = 0)

mcList <- as.list(mc1)
p <- parent.frame()
mcListE <- lapply(mcList, eval, p)

# if simplify found set it and remove it from lists
simplify <- NULL
idx <- match("simplify", tolower(nm), nomatch = 0)
if (idx > 0) {
if (!is.logical(args[[idx]])) {
simplify <- args[[idx]]
args <- args[-idx]
if (!is.logical(mcListE[[idx]])) {
simplify <- mcListE[[idx]]
mcListE <- mcListE[-idx]
mcList <- mcList[-idx]
nm <- nm[-idx]
}
}
is.fo <- sapply(args, function(x) is(x, "formula"))
num.fo <- sum(is.fo)

is.funfo <- is.fo & (num.fo == 1 | seq(along = args) > 1 |
# arg1.idx is the location of argument 1 in mcList
# is.fo is a logical vector indicating whether
# each list element is or is not a formula
# is.funfo is true for formulas to be translated


is.fo <- sapply(mcListE, function(x) is(x, "formula"))
arg1.idx <- 0
if (is(args[[1]], "formula"))
for(i in seq(along = mcListE))
if (is.fo[i] && format(mcList[[i]]) == format(args[[1]]))
arg1.idx <- i
num.fo <- sum(is.fo)
is.funfo <- is.fo & (num.fo == 1 |
seq(along = mcList) != arg1.idx |
nm == "FUN")
mcList <- as.list(mc)[-1]
if (idx > 0) mcList <- mcList[-idx]

for(i in seq(along = args)) {
if (is.fo[i] && (num.fo == 1 || i > 1 || nm[[i]] == "FUN"))
mcList[[i]] <- as.function(args[[i]])
#for(i in seq(along = args)) {
# if (is.fo[i] && (num.fo == 1 || i > 1 || nm[[i]] == "FUN"))
# mcList[[i]] <- as.function(args[[i]])
for(i in seq(along = mcList)) {
if (is.funfo[i]) {
# mcList[[i]] <- as.function(args[[i]])
mcList[[i]] <- as.function(mcListE[[i]])
}
}
# out <- do.call(FUN, args)
out <- do.call(FUN, mcList, env = parent.frame())
Expand All @@ -64,3 +86,5 @@ fn <- structure(NA, class = "fn")
# fn$list(x ~ 2*x)
# fn$mapply(~ x + y, 1:10, 21:30)



5 changes: 4 additions & 1 deletion demo/00Index
@@ -1 +1,4 @@
gsubfn-cut Use strapply to extract endpoints from cut labels
gsubfn-chron read in zoo data with chron datetimes.
gsubfn-gries Stefan Gries' Linguistics code.
gsubfn-cut Use strapply to extract endpoints from cut labels.
gsubfn-si Replace SI scale letter with number,
22 changes: 22 additions & 0 deletions demo/gsubfn-chron.R
@@ -0,0 +1,22 @@

# Use read.zoo to read data with a chron time index
# Ignore fractional seconds.

library(zoo)
library(chron)
library(gsubfn)

# test data
Lines <- "2006-01-24 02:41:24.00011,1.22930000,5,1.22950000,7
2006-01-25 04:41:24.00011,1.22930000,5,1.22950000,7
2006-01-26 07:41:24.00011,1.22930000,5,1.22950000,7"

# convert to chron
to.chron <- function(x)
strapply(format(x), "([0-9-]+) ([0-9:]+)",
~ chron(as.Date(dd), tt), backref = -2,simplify = c)

con <- textConnection(Lines)
read.zoo(con, sep = ",", FUN = to.chron)
close(con)

29 changes: 29 additions & 0 deletions demo/gsubfn-gries.R
@@ -0,0 +1,29 @@

# linguistic applications by Stefan Th. Gries

# create word frequency list from the gsubfn COPYING file

fn1 <- system.file("COPYING", package = "gsubfn")
Lines1 <- tolower(scan(fn1, what = "char", sep = "\n"))
tail(sort(table(unlist(strapply(Lines1, "\\w+", perl = TRUE)))))

# frequency list of words from an SGML-annotated text file
# sampled from the British National Corpus"

fn2 <- system.file("sample.txt", package = "gsubfn")
Lines2 <- scan(fn2, what = "char", sep = "\n")
tagged.corpus.sentences <- grep("^<s n=", Lines2, value = TRUE)
# just to see what it looks like
tagged.corpus.sentences[c(3, 8)]
words <- unlist(strapply(tagged.corpus.sentences, ">([^<]*)", backref = -1))
words <- gsub(" $", "", words)
tail(words, 25)

# frequency list of words AND tags from same file

word.tag.pairs <- unlist(strapply(tagged.corpus.sentences, "<[^<]*"))
cleaned.word.tag.pairs <- grep("<w ", word.tag.pairs, value = TRUE)
cleaned.word.tag.pairs <- gsub(" +$", "", cleaned.word.tag.pairs)
tail(sort(table(cleaned.word.tag.pairs)))
tail(cleaned.word.tag.pairs)

11 changes: 11 additions & 0 deletions demo/gsubfn-si.R
@@ -0,0 +1,11 @@


# given number possibly followed by SI letter (e.g. 32.5k where k means 1000)
# replace letter with e followed by appropriate digits.
# (see formatEng2R by Hans-Joerg Bibiko in the R Wiki)

conv <- c(y = "e-24", z = "e-21", a = "e-18", f = "e-15", p = "e-12",
n = "e-9", `µ` = "e-6", m = "e-3", d = "e-1", c = "e-2", k = "e3",
M = "e6", G = "e9", T = "e12", P = "e15", E = "e18", Z = "e21", Y = "e24")
gsubfn(".$", x ~ if (x %in% names(conv)) conv[x] else x, c("19", "32.5M"))

20 changes: 0 additions & 20 deletions inst/1

This file was deleted.

24 changes: 23 additions & 1 deletion inst/NEWS
@@ -1,5 +1,27 @@

Changes in 0.2-0
Changes in 0.2-1 (development version)

o added "note difference" example in strapply

o letters, LETTERS and pi excluded from args so
x ~ sin(x*pi/180) can be shortened to ~ sin(x*pi/180)
and
x ~ LETTERS[x] can be reduced to ~ LETTERS[x]
(previously it would have added pi and LETTERS to arg
lists in these two examples)

o improved as.function.formula.Rd

o fixes to eliminate R CMD CHECK warnings under R 2.5.0

o bug fix in $.fn. If args and match.call order was different
it could fail previously.

o new demos: gsubfn-gries.R, gsubfn-chron.R, gsubfn-si.R

o THANKS file

Changes in 0.2-0 (latest CRAN release)

o match.funfn

Expand Down
6 changes: 6 additions & 0 deletions inst/THANKS
@@ -0,0 +1,6 @@

Thanks to the following for contributing examples and demos:

Hans-Joerg Bibiko, bibiko at eva.mpg.de
Stefan Th. Gries, stgries at linguistics dot ucsb dot edu

0 comments on commit 29fef4f

Please sign in to comment.