examples/github-issues/qwraps2-issue-0077_reprex.md

Simple answers to both questions, yes, and yes.

Some examples. First we set up a simple summary table

library(qwraps2)
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
options(qwraps2_markup = "markdown")

my_mtcars <- dplyr::select(mtcars, mpg, cyl, hp)
stats_summary <- list("MPG" =
                      list("min" = ~ qwraps2::frmt(min(.data[["mpg"]])),
                           "max" = ~ qwraps2::frmt(max(.data[["mpg"]]))))

st <- summary_table(dplyr::group_by(my_mtcars, cyl), stats_summary)
#> Warning in min(.data[["mpg"]]): no non-missing arguments to min; returning Inf
#> Warning in max(.data[["mpg"]]): no non-missing arguments to max; returning -Inf
#> Warning in min(.data[["mpg"]]): no non-missing arguments to min; returning Inf
#> Warning in max(.data[["mpg"]]): no non-missing arguments to max; returning -Inf
#> Warning in min(.data[["mpg"]]): no non-missing arguments to min; returning Inf
#> Warning in max(.data[["mpg"]]): no non-missing arguments to max; returning -Inf

The default table is

st
#> 
#> 
#> |                 |4 (N = 11)   |6 (N = 7)    |8 (N = 14)   |
#> |:----------------|:------------|:------------|:------------|
#> |**MPG**          |&nbsp;&nbsp; |&nbsp;&nbsp; |&nbsp;&nbsp; |
#> |&nbsp;&nbsp; min |Inf          |Inf          |Inf          |
#> |&nbsp;&nbsp; max |-Inf         |-Inf         |-Inf         |

First question, is it possible to rename the columns, yes. One way is to define the column names via the cname argument of the print method for the summary table.

print(st, cname = c("Four Cylinders", "Six", "Eight"))
#> 
#> 
#> |                 |Four Cylinders |Six          |Eight        |
#> |:----------------|:--------------|:------------|:------------|
#> |**MPG**          |&nbsp;&nbsp;   |&nbsp;&nbsp; |&nbsp;&nbsp; |
#> |&nbsp;&nbsp; min |Inf            |Inf          |Inf          |
#> |&nbsp;&nbsp; max |-Inf           |-Inf         |-Inf         |

Another way to rename the columns, and one that might be safer in a dynamic program is to change the column names of the object. Look at the structure of the st object. It is a character matrix and the column names used by the print method when the cname argument is not provided.

str(st)
#>  'qwraps2_summary_table' chr [1:2, 1:3] "Inf" "-Inf" "Inf" "-Inf" "Inf" ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:2] "min" "max"
#>   ..$ : chr [1:3] "4 (N = 11)" "6 (N = 7)" "8 (N = 14)"
#>  - attr(*, "rgroups")= Named int 2
#>   ..- attr(*, "names")= chr "MPG"
colnames(st)
#> [1] "4 (N = 11)" "6 (N = 7)"  "8 (N = 14)"
colnames(st) <- gsub("cyl", "Cylinders", colnames(st))
colnames(st) <- gsub("\\(.*\\)", "", colnames(st))
st
#> 
#> 
#> |                 |4            |6            |8            |
#> |:----------------|:------------|:------------|:------------|
#> |**MPG**          |&nbsp;&nbsp; |&nbsp;&nbsp; |&nbsp;&nbsp; |
#> |&nbsp;&nbsp; min |Inf          |Inf          |Inf          |
#> |&nbsp;&nbsp; max |-Inf         |-Inf         |-Inf         |

Reordering the columns is a little less intuitive right now. Perhaps I should extend the package features. For now, I would suggest building an factor in your data frame with the levels set in the order you want the columns to appear in.

my_mtcars$my_cyl <- factor(my_mtcars$cyl, levels = c(6, 4, 8), labels = c("Six", "Four", "Eight"))
summary_table(dplyr::group_by(my_mtcars, my_cyl), stats_summary)
#> Warning in min(.data[["mpg"]]): no non-missing arguments to min; returning Inf
#> Warning in max(.data[["mpg"]]): no non-missing arguments to max; returning -Inf
#> Warning in min(.data[["mpg"]]): no non-missing arguments to min; returning Inf
#> Warning in max(.data[["mpg"]]): no non-missing arguments to max; returning -Inf
#> Warning in min(.data[["mpg"]]): no non-missing arguments to min; returning Inf
#> Warning in max(.data[["mpg"]]): no non-missing arguments to max; returning -Inf
#> 
#> 
#> |                 |Six (N = 7)  |Four (N = 11) |Eight (N = 14) |
#> |:----------------|:------------|:-------------|:--------------|
#> |**MPG**          |&nbsp;&nbsp; |&nbsp;&nbsp;  |&nbsp;&nbsp;   |
#> |&nbsp;&nbsp; min |Inf          |Inf           |Inf            |
#> |&nbsp;&nbsp; max |-Inf         |-Inf          |-Inf           |

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

Session info

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Catalina 10.15.6      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Denver              
#>  date     2020-08-27                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source        
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.9      2020-08-24 [1] CRAN (R 4.0.2)
#>  callr         3.4.3      2020-03-28 [1] CRAN (R 4.0.0)
#>  cli           2.0.2      2020-02-28 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 4.0.0)
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.1      2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 4.0.0)
#>  dplyr       * 1.0.2      2020-08-18 [1] CRAN (R 4.0.2)
#>  ellipsis      0.3.1      2020-05-15 [1] CRAN (R 4.0.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 4.0.0)
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.0.2)
#>  generics      0.0.2      2018-11-29 [1] CRAN (R 4.0.0)
#>  glue          1.4.1      2020-05-13 [1] CRAN (R 4.0.0)
#>  highr         0.8        2019-03-20 [1] CRAN (R 4.0.0)
#>  htmltools     0.5.0      2020-06-16 [1] CRAN (R 4.0.0)
#>  knitr         1.29       2020-06-23 [1] CRAN (R 4.0.0)
#>  lifecycle     0.2.0      2020-03-06 [1] CRAN (R 4.0.0)
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 4.0.0)
#>  pillar        1.4.6      2020-07-10 [1] CRAN (R 4.0.2)
#>  pkgbuild      1.1.0      2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.0.0)
#>  pkgload       1.1.0      2020-05-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.3      2020-07-05 [1] CRAN (R 4.0.0)
#>  ps            1.3.4      2020-08-11 [1] CRAN (R 4.0.2)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.0.0)
#>  qwraps2     * 0.4.2.9006 2020-08-26 [1] local         
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5      2020-07-06 [1] CRAN (R 4.0.0)
#>  remotes       2.2.0      2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7      2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3        2020-06-18 [1] CRAN (R 4.0.0)
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.4.6      2020-02-17 [1] CRAN (R 4.0.0)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2      2020-03-02 [1] CRAN (R 4.0.0)
#>  tibble        3.0.3      2020-07-10 [1] CRAN (R 4.0.2)
#>  tidyselect    1.1.0      2020-05-11 [1] CRAN (R 4.0.0)
#>  usethis       1.6.1      2020-04-29 [1] CRAN (R 4.0.0)
#>  vctrs         0.3.2      2020-07-15 [1] CRAN (R 4.0.2)
#>  withr         2.2.0      2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.16       2020-07-24 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library



dewittpe/qwraps2 documentation built on Jan. 4, 2024, 1:59 p.m.