Giter Club home page Giter Club logo

Comments (4)

JauntyJJS avatar JauntyJJS commented on August 18, 2024 1

Hi @jimjam-slam,

Thank you for your recommendations. The suggestion is good enough for me to proceed.
As for the issue with character(0), we shall see how it goes with the purrr developers.

For now I can just manually check if there are any warnings, errors or messages.

from collateral.

jimjam-slam avatar jimjam-slam commented on August 18, 2024

Hi @JauntyJJS! We don't have a specifically named extract_output helper, although I've considered adding one. The problem is mostly just that some people will want list output, while others will want typed vectors. purrr::map and purrr::map_* already has this functionality, so I tend to recommend that people use those:

test %>% mutate(res = map(converted_date_log, "result"))
# # A tibble: 6 × 3
#   test_date  converted_date_log res       
#   <list>     <collat>           <list>    
# 1 <chr [1]>  R _ _ _ _          <date [1]>
# 2 <chr [1]>  R _ _ W _          <date [1]>
# 3 <dttm [1]> R _ _ _ _          <date [1]>
# 4 <lgl [1]>  R _ _ _ _          <lgl [1]> 
# 5 <dbl [1]>  R _ _ W _          <date [1]>
# 6 <chr [1]>  R _ _ W _          <date [1]>

test %>% mutate(res = map_vec(converted_date_log, "result"))
# # A tibble: 6 × 3
#   test_date  converted_date_log res       
#   <list>     <collat>           <date>    
# 1 <chr [1]>  R _ _ _ _          2022-10-25
# 2 <chr [1]>  R _ _ W _          NA        
# 3 <dttm [1]> R _ _ _ _          2022-10-07
# 4 <lgl [1]>  R _ _ _ _          NA        
# 5 <dbl [1]>  R _ _ W _          NA        
# 6 <chr [1]>  R _ _ W _          NA 

(Note that although you're still using purrr twice here, you're at least not running your own function twice—the second use here is just to extract the result).

You should, in theory, be able to extract other components the same way:

test %>% mutate(
  err = map_chr(converted_date_log, c("error", "message"), .default = NA_character_))
# # A tibble: 6 × 3
#   test_date  converted_date_log err  
#   <list>     <collat>           <chr>
# 1 <chr [1]>  R _ _ _ _          NA   
# 2 <chr [1]>  R _ _ W _          NA   
# 3 <dttm [1]> R _ _ _ _          NA   
# 4 <lgl [1]>  R _ _ _ _          NA   
# 5 <dbl [1]>  R _ _ W _          NA   
# 6 <chr [1]>  R _ _ W _          NA

But warnings, messages and output can be a pain, because they return character(0) instead of NULL when there's no such component present:

test %>% mutate(
  warn = map_chr(converted_date_log, "warnings", .default = NA_character_))
# Error in `mutate()`:
# ! Problem while computing `warn =
#   map_chr(converted_date_log, "warnings",
#   .default = NA_character_)`.
# Caused by error in `map_chr()`:
# ℹ In index: 1.
# Caused by error:
# ! Result must be length 1, not 0.
# Run `rlang::last_error()` to see where the error occurred.

That's a bit of a pain, to be frank. I might file an issue with purrr on this, because it's not clear to me why there is a difference between purrr::safely's output and purrr::quietly's output (or why the .default argument of map_chr shouldn't handle character(0) as well). But if they don't want to change it, I might add some helpers for some of these cases!

from collateral.

jimjam-slam avatar jimjam-slam commented on August 18, 2024

I'm also going to at @Maximilian-Stefan-Ernst, because I just spied tidyverse/purrr#843 while checking things for the issue and thought they might like collateral 😛

from collateral.

jimjam-slam avatar jimjam-slam commented on August 18, 2024

Looks like the very issue of dealing with character(0) was addressed in tidyverse/purrr#254, but none of the varieties of argument discussed (.null, .empty, .missing, missing, ...) seem to work.

from collateral.

Related Issues (14)

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.