Comments (18)
+1 (via your Twitter feed). I'll have to think through the specifics more, but I see many use cases.
from magrittr.
Could this be used with with()
to build a proper data =
argument into functions that lack it? That would be a big win.
from magrittr.
I think that it will be very useful to write compact anonymous functions with intermediate computed quantities. Looks great.
from magrittr.
For this purpose I've been using this simple operator lately with good success.
`%within%` <- function(lhs, rhs) {
lhs <- as.list(lhs)
rhs <- substitute(rhs)
eval(rhs, envir = lhs, enclos = parent.frame())
}
@smbache Do you think %within% could be magrittr material?
from magrittr.
Just a remark: %within% exists in lubridate
, and so could cause conflicts
when people want to use lubridate
in combination with dplyr
(which
depends on magrittr
, right?)
On Tue, Jun 17, 2014 at 3:57 PM, lionelgit [email protected] wrote:
@jennybc https://github.com/jennybc
For this purpose I've been using this simple operator lately with good
success.
%within%
<- function(lhs, rhs) {
lhs <- as.list(lhs)
rhs <- substitute(rhs)
eval(rhs, envir = lhs, enclos = parent.frame())
}@smbache https://github.com/smbache Do you think %within% could be
magrittr material?—
Reply to this email directly or view it on GitHub
#18 (comment).
from magrittr.
@jennybc I am not sure exactly what you'd like. Could you give an example of how you imagine this should work?
from magrittr.
@lionelgit Can you give examples of some use cases?
from magrittr.
@jennybc although not relevant for your comment as such, it did make me think of the option to specify which arguments the composed function should accept in addition to the first one. For now, I added ellipses to the signature, so they can be used in the some of the nested functions:
library(dplyr)
library(magrittr)
f <- compose(
x ~ x %>% group_by(Species),
y ~ y %>% do(arrange(., ...)),
z ~ z %>% do(head(., 2))
)
iris %>% f(Sepal.Length)
iris %>% f(Sepal.Width)
I might have a look at more general way of being able to specify the signature of the composed function.
from magrittr.
@holstius Indeed it's difficult to avoid name clashing with the hadleyverse... It's a shame that such an expressive operator is reserved for such a minor function though.
You use it simply like this:
data %within%
lm(col1 ~ col2)
It will be as if you did
attach(data)
lm(col1 ~ col2)
detach(data)
from magrittr.
I have now made the way to add arguments to the composed function a little more flexible:
mae <- compose(abs, x ~ mean(x, na.rm = na.rm),
.args = list(na.rm = TRUE))
The above function can now be written as
f <- compose(
x ~ x %>% group_by(Species),
y ~ y %>% do(arrange(., ...)),
z ~ z %>% do(head(., 2)),
.args = list(...)
)
I think this adds a lot of flexibility in composing the functions.
from magrittr.
@lionelgit Right, the name is a little reversed though, as lm is evaluated within the data, and not the other way around. Perhaps %expose%
as it exposes the data for the rhs.
from magrittr.
I was thinking of it as putting the columns of data inside the next function, but you're right.
Maybe %wrap% ?
from magrittr.
yeah, perhaps :) naming is not always easy.
from magrittr.
I am not sure that an operator is really needed in magrittr for this, as one can just do:
data %>%
with(lm(col1, col2))
The operator does shorten it a bit...
from magrittr.
It's also more expressive. Well, up to you :)
from magrittr.
@smbache Here's a simple example of the sort of usage I mean. The cor()
function lacks both a data =
and subset =
argument. So if I want to compute correlation of two variables in a data.frame for a subset of the rows, it's kind of awkward. Does magrittr
already offer a way to do what I do below less awkwardly? Or is it relevant to this conversation about composition? Either way, I'll be happy!
gdURL <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
gDat <- read.delim(file = gdURL)
str(gDat) # 'data.frame': 1704 obs. of 6 variables:
with(subset(gDat, subset = country == "Colombia"), cor(lifeExp, gdpPercap))
# [1] 0.9514699
from magrittr.
@jennybc @lionelgit I added a version of the suggested wrap/expose operator under the name %$%
, where the rationale of the name is that you get to not write data$
in front of the names. You could then do, say
iris %$% cor(Sepal.Length, Sepal.Width)
or
iris %>%
subset(Species == "setosa") %$%
cor(Sepal.Length, Sepal.Width)
As to existing options, in combination with dplyr
, you could do
iris %>%
filter(Species == "setosa") %>%
summarise(Correlation = cor(Sepal.Length, Sepal.Width))
or use a lambda/anonymous function.
I am not sure whether it will be included in the next version, I also have to consider the opinion of my co-author of the package. But it is sort of handy, I admit. :)
from magrittr.
The %$% is a great idea. And for those using a functional editor, you can use this unicode character which conveys the idea of sending the cols in the function that follows :)
⇉
from magrittr.
Related Issues (20)
- Feature request: Assignment expression operator HOT 1
- Move `master` branch to `main` HOT 1
- `magrittr::freduce()` doesn't support lambda functions HOT 4
- Add warning when piped object overwrites keyword arguments when not used as first argument HOT 1
- Inconsistent assign() behavior in simple piping with Magrittr HOT 1
- Bug: Rstudio completion with "mrgmod" S4 class object HOT 4
- Feature: Function to create functional sequences HOT 1
- Suggest alias for `Encoding<-` HOT 1
- Hartmann pipelines HOT 3
- Magrittr Logo HOT 1
- difference in performance HOT 2
- Different results using pipes HOT 2
- introspection HOT 2
- magrittr upkeep 2023
- Add function not behaving correctly when being fed an expression
- Pipe doesn't work in basic round function HOT 1
- Question: magrittr's pipes not hyperlinked in docs, why not?
- Question: UN or UNE pipe? HOT 1
- Assignment pipe `%<>%` breaks if R-native `|>` pipes used in chain instead of magrittr-native pipes `%>%` HOT 1
- How to quote magrittr library/package in function call: `magrittr::%<>%` doesn't work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magrittr.