Upgrade to Pro — share decks privately, control downloads, hide ads and more …

rap

 rap

Presentation of the rap package to the tidyverse team

Romain François

December 03, 2018
Tweet

More Decks by Romain François

Other Decks in Technology

Transcript

  1. iterate <- function(.tbl, ...) { # capture dots dots <-

    enexprs(...) # prep named fn list. this will get !!! into the mutate call fns <- new_list(length(dots), names(dots)) for(i in seq_along(dots)) { nms <- colnames(.tbl) args <- rep(list(rlang::missing_arg()), length(nms)) names(args) <- nms dot <- dots[[i]] fn <- new_function(args, dot) fns[[i]] <- expr(pmap(.tbl, !! fn)) .tbl <- mutate(.tbl, !!!fns[i]) } .tbl }
  2. library(gapminder) library(tidyverse) # setup gap_nested <- gapminder %>% filter(continent ==

    "Asia") %>% mutate( yr1952 = year - 1952, country = fct_drop(country) ) %>% group_by(country) %>% group_nest() # mutate + *map* gap_fitted <- gap_nested %>% mutate( fit = map (data, ~ lm(lifeExp ~ yr1952, data = .x)), intercept = map_dbl(fit, ~ coef(.x)[["(Intercept)"]]), slope = map_dbl(fit, ~ coef(.x)[["yr1952"]]) )
  3. gap_nested %>% mutate( fit = map (data, ~ lm(lifeExp ~

    yr1952, data = .x)), intercept = map_dbl(fit, ~ coef(.x)[["(Intercept)"]]), slope = map_dbl(fit, ~ coef(.x)[["yr1952"]]) ) Names of the new columns Types of the new columns (empty means list) What happens What is being iterated on
  4. *map*() • Iterate on 1 thing: • Iterate on 2

    things: • Iterate on more than 2 things map[_type](<thing>, ~fun(.x)) map2[_type](<thing1>, <thing2>, ~fun(.x, .y)) pmap[_type]( list(<thing1>, <thing2>, <...>, <thing_n>), function(<name1>, <name2>, <...>, <name_n>) { fun(<name1>, <name2>, <...>, <name_n>) } )
  5. gap_fitted <- gap_nested %>% rap( fit = ~ lm(lifeExp ~

    yr1952, data = data), intercept = double() ~ coef(fit)[["(Intercept)"]], slope = double() ~ coef(fit)[["yr1952"]] ) Names of the new columns Types of the new columns (empty means list) Expression for a single row of gap_nested rap() iterates on rows of gap_nested
  6. gap_nested %>% mutate( fit = map (data, ~ lm(lifeExp ~

    yr1952, data = .x)), intercept = map_dbl(fit, ~ coef(.x)[["(Intercept)"]]), slope = map_dbl(fit, ~ coef(.x)[["yr1952"]]) ) gap_fitted <- gap_nested %>% rap( fit = ~ lm(lifeExp ~ yr1952, data = data), intercept = double() ~ coef(fit)[["(Intercept)"]], slope = double() ~ coef(fit)[["yr1952"]] ) rap() mutate() + *map*()
  7. Bonus track: make data frame columns with rap() gap_fitted <-

    gap_nested %>% rap( fit = ~ lm(lifeExp ~ yr1952, data = data), coefs = data.frame() ~ tibble(!!!coef(fit)) )
  8. library(rap) library(tibble) tbl <- tibble(x = 1:3, y = letters[1:3],

    z = tibble(a = 1:3, b = 1:3)) fun <- lap(tbl, double() ~ x + y + z$a + z$b) formals(fun) #> $`.::index::.` #> #> #> $x #> .subset2(1:3, `.::index::.`) #> #> $y #> .subset2(c("a", "b", "c"), `.::index::.`) #> #> $z #> list(a = 1:3, b = 1:3)[`.::index::.`, , drop = FALSE] #> #> $..data #> environment() #> #> $..env #> <environment: package:tibble> #> attr(,"name") #> [1] "package:tibble" #> attr(,"path") #> [1] "/Library/Frameworks/R.framework/Versions/3.5/Resources/library/tibble" body(fun) #> { #> rlang::eval_tidy(x + y + z$a + z$b) #> }