njtierney / naniar

Tidy data structures, summaries, and visualisations for missing data
http://naniar.njtierney.com/
Other
650 stars 54 forks source link

Issue with `coerce_fct_na_explicit` #251

Closed njtierney closed 1 year ago

njtierney commented 4 years ago

As described in #250 - just adding an issue here for future reference as I think there might still be another bug in gg_miss_fct.

library(naniar)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

dat_weather <- data.frame(
  date = c("2017-01-02","2017-01-03","2017-01-04","2017-01-05","2017-01-06",
           "2017-01-07"),
  month = c(1,1,1,1,2,2),
  temp_min = c(5.9,8.4,10.4,5.8,4.4,NA),
  temp_max = c(10.5,11.1,14.5,9.4,9.1, NA),
  temp_avg = c(8.4,9.7,12.4,8.1,6.4,NA),
  wind_speed_max = c(5.1,5.1,6.7,5.1,5.1, NA)
)  

# options(error = recover)
gg_miss_fct(dat_weather, fct = month)
#> Error: Column `month` is unknown

Created on 2020-03-03 by the reprex package (v0.3.0)

Should be resolved by #250 - below is the result when building the pkg under #250

library(naniar)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

dat_weather <- data.frame(
  date = c("2017-01-02","2017-01-03","2017-01-04","2017-01-05","2017-01-06",
           "2017-01-07"),
  month = c(1,1,1,1,2,2),
  temp_min = c(5.9,8.4,10.4,5.8,4.4,NA),
  temp_max = c(10.5,11.1,14.5,9.4,9.1, NA),
  temp_avg = c(8.4,9.7,12.4,8.1,6.4,NA),
  wind_speed_max = c(5.1,5.1,6.7,5.1,5.1, NA)
)  

# options(error = recover)
gg_miss_fct(dat_weather, fct = month)


# error seems to persist when coercing to factor, however?
gg_miss_fct(dat_weather, fct = as.factor(month))
#> Error: object 'month' not found

Created on 2020-03-03 by the reprex package (v0.3.0)

njtierney commented 1 year ago

This is all fine - writing as.factor(month) in there plays havoc with how we parse the bare names of months - if you want to treat it as a factor as is, this is fine.

library(naniar)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

dat_weather <- data.frame(
  date = c("2017-01-02","2017-01-03","2017-01-04","2017-01-05","2017-01-06",
           "2017-01-07"),
  month = c(1,1,1,1,2,2),
  temp_min = c(5.9,8.4,10.4,5.8,4.4,NA),
  temp_max = c(10.5,11.1,14.5,9.4,9.1, NA),
  temp_avg = c(8.4,9.7,12.4,8.1,6.4,NA),
  wind_speed_max = c(5.1,5.1,6.7,5.1,5.1, NA)
)  

gg_miss_fct(dat_weather, fct = month)


dat_weather %>% 
  mutate(month = as.factor(month)) %>% 
  gg_miss_fct(fct = month)


# error persists internally when coercing to factor due to how 
# `fct` is parsed
gg_miss_fct(dat_weather, fct = as.factor(month))
#> Error in `tbl_at_vars()`:
#> ! Problem while evaluating `as.factor(month)`.
#> Caused by error in `is.factor()`:
#> ! object 'month' not found
#> Backtrace:
#>      ▆
#>   1. ├─naniar::gg_miss_fct(dat_weather, fct = as.factor(month))
#>   2. │ └─... %>% miss_var_summary() at naniar/R/gg-miss-fct.R:29:2
#>   3. ├─naniar::miss_var_summary(.)
#>   4. │ └─naniar:::test_if_null(data) at naniar/R/miss-x-summary.R:47:2
#>   5. ├─dplyr::group_by(., !!fct) at naniar/R/miss-x-summary.R:47:2
#>   6. ├─dplyr::mutate_at(., vars(!!fct), .funs = coerce_fct_na_explicit)
#>   7. │ └─dplyr:::manip_at(...)
#>   8. │   └─dplyr:::tbl_at_syms(...)
#>   9. │     └─dplyr:::tbl_at_vars(tbl, vars, .include_group_vars = .include_group_vars, error_call = error_call)
#>  10. │       └─tidyselect::vars_select(tibble_vars, !!!vars)
#>  11. │         └─tidyselect:::eval_select_impl(...)
#>  12. │           ├─tidyselect:::with_subscript_errors(...)
#>  13. │           │ └─rlang::try_fetch(...)
#>  14. │           │   └─base::withCallingHandlers(...)
#>  15. │           └─tidyselect:::vars_select_eval(...)
#>  16. │             └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#>  17. │               └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  18. │                 └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  19. │                   └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  20. │                     └─tidyselect:::eval_context(expr, context_mask, call = error_call)
#>  21. │                       ├─tidyselect:::with_chained_errors(...)
#>  22. │                       │ └─rlang::try_fetch(...)
#>  23. │                       │   ├─base::tryCatch(...)
#>  24. │                       │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  25. │                       │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  26. │                       │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  27. │                       │   └─base::withCallingHandlers(...)
#>  28. │                       └─rlang::eval_tidy(as_quosure(expr, env), context_mask)
#>  29. ├─base::as.factor(month)
#>  30. │ └─base::is.factor(x)
#>  31. └─base::.handleSimpleError(...)
#>  32.   └─rlang (local) h(simpleError(msg, call))
#>  33.     └─handlers[[1L]](cnd)
#>  34.       └─rlang::abort(msg, call = call, parent = cnd)

Created on 2023-04-10 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.3 (2023-03-15) #> os macOS Ventura 13.2 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Australia/Hobart #> date 2023-04-10 #> pandoc 2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.0) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.2.0) #> curl 5.0.0 2023-01-12 [1] CRAN (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.0) #> dplyr * 1.1.1 2023-03-22 [1] CRAN (R 4.2.0) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.0) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.0) #> farver 2.1.1 2022-07-06 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.0) #> ggplot2 3.4.1 2023-02-10 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> gridExtra 2.3 2017-09-09 [1] CRAN (R 4.2.0) #> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.0) #> highr 0.10 2022-12-22 [1] CRAN (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.0) #> httr 1.4.4 2022-08-17 [1] CRAN (R 4.2.0) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.0) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> naniar * 1.0.0.9000 2023-04-10 [1] local #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.2.0) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> styler 1.9.0 2023-01-15 [1] CRAN (R 4.2.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.2.0) #> tidyr 1.3.0 2023-01-24 [1] CRAN (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.0) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.0) #> vctrs 0.6.1 2023-03-22 [1] CRAN (R 4.2.0) #> viridis 0.6.2 2021-10-13 [1] CRAN (R 4.2.0) #> viridisLite 0.4.1 2022-08-22 [1] CRAN (R 4.2.0) #> visdat 0.6.0 2023-02-02 [1] local #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.37 2023-01-31 [1] CRAN (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```