Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crosstable doesn't properly show N in header when using multiple by variables and removing header keys #21

Closed
bberger94 opened this issue Sep 7, 2022 · 2 comments
Labels
bug 🐞 Something isn't working

Comments

@bberger94
Copy link

Describe the bug
When using multiple by variables and removing header keys there are two issues.

First, the key for the first by variable is truncated to only display the text after N=. So if the text should read "right (N=232)", instead it just reads "232)".

Second, the key for the other by variable isn't removed.

Reproducible example

library(crosstable)


set.seed(1)
n <- 1000
data <- data.frame(
  g1 = ifelse(runif(n) > 0.5, "high", "low"),
  g2 = ifelse(runif(n) > 0.5, "right", "left")
)

data$y <- as.integer(data$g1 == "high") + rnorm(n)

crosstable(data, 
           cols = y,
           by = c(g2, g1)) %>% 
  as_flextable(remove_header_keys = TRUE,
               header_show_n = TRUE)

Session info

``` r R version 4.1.2 (2021-11-01) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Monterey 12.3.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base

other attached packages:
[1] crosstable_0.5.0.9002

loaded via a namespace (and not attached):
[1] zip_2.2.0 Rcpp_1.0.9 pillar_1.8.1
[4] compiler_4.1.2 forcats_0.5.2 base64enc_0.1-3
[7] tools_4.1.2 digest_0.6.29 uuid_1.0-3
[10] evaluate_0.16 lifecycle_1.0.1 tibble_3.1.8
[13] checkmate_2.1.0 pkgconfig_2.0.3 rlang_1.0.4
[16] DBI_1.1.1 cli_3.3.0 rstudioapi_0.13
[19] xfun_0.32 fastmap_1.1.0 officer_0.4.2
[22] dplyr_1.0.9 stringr_1.4.1 knitr_1.40
[25] xml2_1.3.3 generics_0.1.3 gdtools_0.2.4
[28] vctrs_0.4.1 systemfonts_1.0.4 tidyselect_1.1.2
[31] glue_1.6.2 data.table_1.14.2 R6_2.5.1
[34] fansi_1.0.3 rmarkdown_2.16 tidyr_1.2.0
[37] purrr_0.3.4 magrittr_2.0.3 ellipsis_0.3.2
[40] backports_1.4.1 htmltools_0.5.3 assertthat_0.2.1
[43] flextable_0.7.0 utf8_1.2.2 stringi_1.7.8
[46] crayon_1.4.2

</details>
@bberger94 bberger94 added the bug 🐞 Something isn't working label Sep 7, 2022
@DanChaltiel
Copy link
Owner

Thanks for the feedback, this is a simple problem of a greedy regex, I always stumble across these.
This should be quick to solve.

@bberger94
Copy link
Author

bberger94 commented Sep 8, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants