ColinFay / attempt

Tools for defensive programming in R
Other
124 stars 12 forks source link

variant of map_try_catch_df that returns a list of dfs #13

Open MilesMcBain opened 5 years ago

MilesMcBain commented 5 years ago

I propose a variant of map_try_catch_df that returns a list of 1 row tibbles instead of a single df.

For argument's sake it could be called map_try_catch_ltbl This would facilitate usage inside tidy modelling pipelines e.g.

  train_data %>%
  group_by(group_var) %>%
  nest() %>%
  mutate(result = map_try_catch_ltbl(data, fit_model)) %>%
  unnest(result)

## resulting tibble now contains call, error, warning, value columns.

Helpful arguments I can think of:

Possible extension: Also allow capturing of messages?

MilesMcBain commented 5 years ago

I've started on an implementation and have a better working example in README here: https://github.com/MilesMcBain/noprobs

ColinFay commented 5 years ago

Hey Miles,

Thanks, that's indeed a very nifty idea!

For the map / nest thing, I think you can already do this with try_catch_df()

library(titanic)
library(tidyverse)
library(attempt)
#> 
#> Attaching package: 'attempt'
#> The following object is masked from 'package:dplyr':
#> 
#>     if_else

fitting_fn <- function(a_df){
  glm(Survived ~ ., family = binomial, data = a_df)
}

titanic_train %>%
  group_by(Pclass) %>%
  nest() %>%
  mutate(result = map(data, ~try_catch_df(fitting_fn(.x)))) %>%
  unnest(result)
#> # A tibble: 3 x 6
#>   Pclass data           call        error warning                   value  
#>    <int> <list>         <chr>       <lgl> <chr>                     <list> 
#> 1      3 <tibble [491 … fitting_fn… NA    glm.fit: algorithm did n… <S3: g…
#> 2      1 <tibble [216 … fitting_fn… NA    glm.fit: algorithm did n… <S3: g…
#> 3      2 <tibble [184 … fitting_fn… NA    glm.fit: algorithm did n… <S3: g…

Created on 2019-06-07 by the reprex package (v0.2.1)

What you've got though (and which indeed misses in {attempt} is something that returns the message, so far only errors and warnings are returned.

Also, you return a list of warning and a list of error, which seems to be a better implementation as it allows to return mutliple warnings 💪


For the map_try_catch_ltbl, I think we can get what you want by just removing thedo.call(rbind()) from map_try_catch_df(). The implementation you've made with the withCallingHandlers is indeed a better way to go, so that we can get the message out of it.

> attempt::map_try_catch_df
function (l, fun) 
{
    do.call(rbind, lapply(l, function(x) eval(try_catch_df_builder(x, 
        fun))))
}
library(attempt)
map_try_catch_ltbl <- function (l, fun) {
  lapply(l, function(x) eval(attempt:::try_catch_df_builder(x, fun)))
}

map_try_catch_ltbl(list(1, "a", 2), log)
#> [[1]]
#>                   call error warning value
#> 1 .Primitive("log")(1)    NA      NA     0
#> 
#> [[2]]
#>                     call                                         error
#> 1 .Primitive("log")("a") non-numeric argument to mathematical function
#>   warning value
#> 1      NA error
#> 
#> [[3]]
#>                   call error warning     value
#> 1 .Primitive("log")(2)    NA      NA 0.6931472

Created on 2019-06-07 by the reprex package (v0.2.1)

Also, I love your any_problems idea!

Would you be willing to PR this into {attempt} (and of course list you as ctb) ?

MilesMcBain commented 5 years ago

Yes I'd be willing to PR! :) Do you like that example name? I don't really it was just the first thing I could think of.