Comments (4)
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.
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.
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.
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)
- `pmap` variants not handling `.l` properly HOT 3
- Column classes don't survive dplyr::bind_rows (or joins?) HOT 1
- Refresh and solicit feedback on vignettes
- Add example datasets
- Integration with reticulate
- Coloured output in README not being converted to HTML
- collateral not on cran anymore? HOT 7
- Knitted output HOT 3
- Integration with furrr HOT 1
- Add safely() arguments HOT 1
- Helpers for extracting side effects HOT 2
- feature request: handle errors and warnings together peacfully HOT 8
- how to combine `quietly_mapped` and `safely_mapped` outputs? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from collateral.