Code Monkey home page Code Monkey logo

Comments (18)

dholstius avatar dholstius commented on June 4, 2024

+1 (via your Twitter feed). I'll have to think through the specifics more, but I see many use cases.

from magrittr.

jennybc avatar jennybc commented on June 4, 2024

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.

lionel- avatar lionel- commented on June 4, 2024

I think that it will be very useful to write compact anonymous functions with intermediate computed quantities. Looks great.

from magrittr.

lionel- avatar lionel- commented on June 4, 2024

@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 Do you think %within% could be magrittr material?

from magrittr.

dholstius avatar dholstius commented on June 4, 2024

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.

smbache avatar smbache commented on June 4, 2024

@jennybc I am not sure exactly what you'd like. Could you give an example of how you imagine this should work?

from magrittr.

smbache avatar smbache commented on June 4, 2024

@lionelgit Can you give examples of some use cases?

from magrittr.

smbache avatar smbache commented on June 4, 2024

@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.

lionel- avatar lionel- commented on June 4, 2024

@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.

@jennybc @smbache

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.

smbache avatar smbache commented on June 4, 2024

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.

smbache avatar smbache commented on June 4, 2024

@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.

lionel- avatar lionel- commented on June 4, 2024

I was thinking of it as putting the columns of data inside the next function, but you're right.

Maybe %wrap% ?

from magrittr.

smbache avatar smbache commented on June 4, 2024

yeah, perhaps :) naming is not always easy.

from magrittr.

smbache avatar smbache commented on June 4, 2024

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.

lionel- avatar lionel- commented on June 4, 2024

It's also more expressive. Well, up to you :)

from magrittr.

jennybc avatar jennybc commented on June 4, 2024

@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.

smbache avatar smbache commented on June 4, 2024

@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.

lionel- avatar lionel- commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.