Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37559b6

Browse files
author
Jonathan Chang
committed
first commit
0 parents  commit 37559b6

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

Rpipe.R

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!Rscript
2+
3+
################################
4+
# Wrappers for common operations
5+
################################
6+
7+
Rpipe.group <- function(d, key, expr) {
8+
expr <- substitute(expr)
9+
key <- eval(substitute(key), d)
10+
do.call(rbind,
11+
by(d, key, function(x) Rpipe.eval(x, expr)))
12+
}
13+
14+
Rpipe.aggregate <- function(d, key, ...) {
15+
key.name <- deparse(substitute(key))
16+
key <- eval(substitute(key), d)
17+
expr <- substitute(list(...))
18+
do.call(rbind,
19+
by(cbind(d, .key = key), key, function(x) {
20+
do.call(data.frame,
21+
c(structure(list(x[1, ".key"]),
22+
names = key.name),
23+
eval(expr, x)))
24+
}))
25+
}
26+
27+
Rpipe.sort <- function(d, ..., decreasing = FALSE) {
28+
expr <- substitute(list(...))
29+
d[do.call("order", c(eval(expr, d), decreasing = decreasing)),]
30+
}
31+
32+
################################
33+
# Core operations
34+
################################
35+
36+
Rpipe.prefix <- function(e1) {
37+
UseMethod("Rpipe.prefix")
38+
}
39+
40+
Rpipe.prefix.default <- function(e1) {
41+
c("Rpipe.", "")
42+
}
43+
44+
Rpipe.eval <- function(e1, expr) {
45+
if (as.character(expr[1]) == "(") {
46+
do.call(`%|%`, list(e1 = e1, e2 = expr[[2]]))
47+
} else if (as.character(expr[1]) == "%|%") {
48+
left <- do.call(`%|%`, list(e1 = e1, e2 = expr[[2]]))
49+
do.call(`%|%`, list(e1 = left, e2 = expr[[3]]))
50+
} else if (as.character(expr[1]) == "%&&%") {
51+
structure(list(e1 = do.call(`%|%`, list(e1 = e1, e2 = expr[[2]])),
52+
e2 = do.call(`%|%`, list(e1 = e1, e2 = expr[[3]]))),
53+
class = "Rpipe.tuple")
54+
} else {
55+
prefixes <- Rpipe.prefix(e1)
56+
func.names <- paste(prefixes, expr[1], sep = "")
57+
lookups <- mget(func.names,
58+
envir = as.environment(-1),
59+
ifnotfound = list(NULL),
60+
inherits = TRUE)
61+
func.names <- lookups[-which(sapply(lookups, is.null))]
62+
if (length(func.names) == 0) {
63+
stop(paste("Could not find pipe function", expr[1]))
64+
}
65+
do.call(names(func.names)[1],
66+
c(list(e1), as.list(expr[-1])))
67+
}
68+
}
69+
70+
`%|%` <- function(e1, e2) {
71+
expr <- substitute(e2)
72+
Rpipe.eval(e1, expr)
73+
}
74+

Rpipe.Rd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
\name{Rpipe}
2+
\alias{Rpipe}
3+
\title{
4+
Manipulate a data frame through a series of pipe operations.
5+
}
6+
\description{
7+
8+
}
9+
\usage{
10+
df %|% operation()
11+
}
12+
\arguments{
13+
\item{df}{
14+
A data frame to operate on. Note that this can be the output of another \code{Rpipe} stage.}
15+
\item{e2}{
16+
%% ~~Describe \code{e2} here~~
17+
}
18+
}
19+
\details{
20+
%% ~~ If necessary, more details than the description above ~~
21+
}
22+
\value{
23+
%% ~Describe the value returned
24+
%% If it is a LIST, use
25+
%% \item{comp1 }{Description of 'comp1'}
26+
%% \item{comp2 }{Description of 'comp2'}
27+
%% ...
28+
}
29+
\author{
30+
Jonathan Chang <jonchang@fb.com>
31+
}
32+
33+
\examples{
34+
iris %|%
35+
subset(Sepal.Width > 3.0) %|%
36+
sort(Sepal.Length) %|%
37+
group(Species, head()) %|%
38+
aggregate(Species, Mean.Petal.Width = mean(Petal.Width))
39+
}

Rpipe.jjplot.R

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
################################
2+
# Initial Rpipe interface for jjplot
3+
################################
4+
5+
## Need to do checks on e1 = jjplot.state?
6+
## both jjplot.stat.foo and jjplot.geom.foo
7+
8+
Rpipe.prefix.jjplot.state <- function(e1) {
9+
c("jjplot.stat",
10+
"jjplog.geom",
11+
"Rpipe",
12+
"")
13+
}
14+
15+
jjplot.data <- function(data, ...) {
16+
structure(list(data = transform(data, ...)),
17+
class = "jjplot.state")
18+
}
19+
20+
jjplot.range <- function(state, ...) {
21+
list(x = range(state$data$x),
22+
y = range(state$data$y))
23+
}
24+
25+
jjplot.geom <- function(plot.function,
26+
range.function = jjplot.range) {
27+
## Add extra info here!
28+
function(state, ...) {
29+
structure(list(plot.function = plot.function,
30+
range = range.function(state, ...),
31+
state = state),
32+
class = "jjplot.goem")
33+
}
34+
}
35+
36+
## Logic for scales:
37+
## Two aspects
38+
## 1.) ticks
39+
## 2.) range
40+
## range should be union of numeric ranges
41+
## ticks depend on type
42+
## numeric - pretty
43+
## factor - at ticks
44+
## cast numeric -> factor
45+
## check if factors equal.
46+
47+
jjplot.data(iris) %|%
48+
aggregate(Species, Mean.Sepal.Width = mean(Sepal.Width)) %|%
49+
transform(x = Species, y = Mean.Sepal.Width)

0 commit comments

Comments
 (0)