Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement preduce? #163

Closed
hadley opened this issue Jan 12, 2016 · 3 comments
Closed

Implement preduce? #163

hadley opened this issue Jan 12, 2016 · 3 comments
Labels

Comments

@hadley
Copy link
Member

@hadley hadley commented Jan 12, 2016

Parallel reduce would allow you to specific arguments that varied across each call. Would be useful if you want to collapse many joins. Need to think if we could have some convention for naming input elements and matching with arguments.

@hadley hadley added the feature label Mar 3, 2017
@hadley
Copy link
Member Author

@hadley hadley commented Mar 8, 2017

Note that the presence of init changes how the arguments align:

out <- init
out <- f(out, x[[1]], y[[1]])
out <- f(out, x[[2]], y[[2]])

out <- x[[1]]
out <- f(out, x[[2]], y[[1]])
out <- f(out, x[[3]], y[[2]])

To match pmap() the call might look like:

preduce(list(dfs, vars), left_join, ...) 

but vars would have length one less than dfs (which makes sense because of fence-posting). Maybe also need reduce2():

reduce2(dfs, vars, left_join, ...)

Maybe just start with reduce2() and leave preduce() for now? Seems harder to imagine use case given reduce2().

Loading

@hadley hadley closed this in 2d7fd45 Mar 8, 2017
@Ljupch0
Copy link

@Ljupch0 Ljupch0 commented May 2, 2021

In terms of use case, I reach for reduce functions when adding cumulatively to an initial object. For instance with the officer package you can generate a powerpoint presentation. You start with an empty presentation and every function call adds a slide to the cumulative presentation. So instead of using pmap that would generate 50 presentations, I've needed a single presentation with 50 slides based on the same template, which sounds like preduce. The init is the starting empty template, and then every function call adds a slide. Some pseudo code:

preduce <- function(.init, .l, .f, ... ) {
  # smart code
}

preduce(.init = starting_template, .l = data, .f = add_slide) 

#would be
  starting_template %>% 
    add_slide(..1, ..2, ...) %>% 
    add_slide(..1, ..2, ...) %>% 
    add_slide(..1, ..3, ...) %>% ...

I have working code of this example here. I needed to iterate a single vector so just reduce worked, but a more general preduce would be fantastic: https://github.com/Ljupch0/stock_presentations/blob/master/Multiple%20Tickers.RMd

Loading

@moodymudskipper
Copy link
Contributor

@moodymudskipper moodymudskipper commented May 31, 2021

I've been needing this too, the following seems to work fine :

preduce <- function(.l,.f, ..., .init, .dir = c("forward", "backward")) {
  .dir <- match.arg(.dir)
  reduce(transpose(.l), function(x,y) exec(.f, x, !!!y, ...), .init = .init, .dir = .dir)
}

.init is mandatory here, the intended use case is to have a data.frame of arguments to apply row-wise in sequence on an object passed through .init

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants