vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
896 stars 74 forks source link

Error in `datasummary()` when the data have a labelled variable but without variable label #752

Closed marklhc closed 3 months ago

marklhc commented 3 months ago

I was working on a SPSS data set where one of the variables has value labels but not a variable label, and I got an error when trying to run datasummary(). The error seems to occur in datasummary_extract(). Here's a small example to reproduce the error:

library(labelled)
library(modelsummary)
#> `modelsummary` 2.0.0 now uses `tinytable` as its default table-drawing
#>   backend. Learn more at: https://vincentarelbundock.github.io/tinytable/
#> 
#> Revert to `kableExtra` for one session:
#> 
#>   options(modelsummary_factory_default = 'kableExtra')
#> 
#> Change the default backend persistently:
#> 
#>   config_modelsummary(factory_default = 'gt')
#> 
#> Silence this message forever:
#> 
#>   config_modelsummary(startup_message = FALSE)
# v1 has variable label
v1 <- labelled(
    c(0L, 0L, 1L),
    labels = c(yes = 1, no = 0),
    label = "V1"
)
data.frame(v1, y = 1:3) |>
    datasummary(formula = y ~ Mean)
Mean
y 2.00
# v2 has no variable label
v2 <- labelled(
    c(0L, 0L, 1L),
    labels = c(yes = 1, no = 0)
)
data.frame(v2, y = 1:3) |>
    datasummary(formula = y ~ Mean)
#> Error in if (nrow(header) == 1) {: argument is of length zero

Created on 2024-04-18 with reprex v2.1.0

Session info ``` r sessionInfo() #> R version 4.3.3 (2024-02-29) #> Platform: x86_64-pc-linux-gnu (64-bit) #> Running under: Ubuntu 22.04.4 LTS #> #> Matrix products: default #> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 #> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0 #> #> locale: #> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C #> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 #> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 #> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C #> [9] LC_ADDRESS=C LC_TELEPHONE=C #> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C #> #> time zone: America/Los_Angeles #> tzcode source: system (glibc) #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> other attached packages: #> [1] modelsummary_2.0.0.4 labelled_2.12.0 #> #> loaded via a namespace (and not attached): #> [1] vctrs_0.6.5 cli_3.6.2 knitr_1.45 rlang_1.1.3 #> [5] xfun_0.41 forcats_1.0.0 haven_2.5.4 generics_0.1.3 #> [9] data.table_1.15.4 glue_1.7.0 backports_1.4.1 htmltools_0.5.7 #> [13] hms_1.1.3 fansi_1.0.6 rmarkdown_2.26 evaluate_0.23 #> [17] tibble_3.2.1 tinytable_0.2.1.12 fastmap_1.1.1 yaml_2.3.8 #> [21] lifecycle_1.0.4 insight_0.19.10 compiler_4.3.3 dplyr_1.1.4 #> [25] fs_1.6.3 pkgconfig_2.0.3 digest_0.6.34 R6_2.5.1 #> [29] reprex_2.1.0 tidyselect_1.2.1 utf8_1.2.4 pillar_1.9.0 #> [33] magrittr_2.0.3 checkmate_2.3.1 tools_4.3.3 withr_3.0.0 #> [37] tables_0.9.17 ```

Thanks!

vincentarelbundock commented 3 months ago

Thanks a lot for the useful reproducible example.

This should be fixed in the development version on Github.