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

different behavior b/w modify_depth and at_depth #363

Closed
lmwang9527 opened this issue Aug 4, 2017 · 1 comment
Closed

different behavior b/w modify_depth and at_depth #363

lmwang9527 opened this issue Aug 4, 2017 · 1 comment

Comments

@lmwang9527
Copy link

lmwang9527 commented Aug 4, 2017

With at_depth in purrr pre-0.2.3, it is possible to run the following code, but modify_depth breaks the code as it tries to modify .x. I understand modify_depth may not be intended to be backward-compatible, but is there an alternative function with at_depth like behavior in 0.2.3?

Thanks for the powerful package and the improvement in the new version!

# See https://stackoverflow.com/a/43861746/688693 for more details
devtools::install_version("purrr", version = "0.2.2.2", repos = "http://cran.us.r-project.org")

library(gapminder)
library(dplyr)
library(tidyr)
library(purrr)
library(broom)

fmlas <- tibble::tribble(
  ~continent, ~formula,
  "Asia", ~lm(lifeExp ~ year, data = .),
  "Europe", ~lm(lifeExp ~ year + pop, data = .),
  "Africa", ~lm(lifeExp ~ year + gdpPercap, data = .),
  "Americas", ~lm(lifeExp ~ year - 1, data = .),
  "Oceania", ~lm(lifeExp ~ year + pop + gdpPercap, data = .)
)

by_continent <- gapminder %>% 
  nest(-continent) %>%
  left_join(fmlas) %>%
  mutate(model=map2(data, formula, ~at_depth(.x, 0, .y)))

by_continent %>% 
  mutate(glance=map(model, glance)) %>% 
  unnest(glance, .drop=T)

detach(package:purrr, unload=TRUE)
devtools::install_version("purrr", version = "0.2.3", repos = "http://cran.us.r-project.org")
library(purrr)

by_continent <- gapminder %>% 
  nest(-continent) %>%
  left_join(fmlas) %>%
  mutate(model=map2(data, formula, ~modify_depth(.x, 0, .y)))
# Error in mutate_impl(.data, dots) : 
#   Evaluation error: replacement element 7 has 5 rows, need 396.

## OR
by_continent <- gapminder %>% 
  nest(-continent) %>%
  left_join(fmlas) %>%
  mutate(model=map2(data, formula, ~modify(.x, .y)))
#Error in mutate_impl(.data, dots) : 
#  Evaluation error: object 'lifeExp' not found.
@hadley
Copy link
Member

hadley commented Feb 5, 2018

Duplicate of #381

@hadley hadley marked this as a duplicate of #381 Feb 5, 2018
@hadley hadley closed this as completed Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants