rsummaryqwraps2

"Error: `x` must be a formula" with qwraps2 summary_table function


I am trying to make a table with summary statistics that looks similar to this (but with min/max/median/mean values filled in):

                            Type
Mass (g)        tct    tcx    tht    thx    tct
    Min
    Max
    Median
    Mean (SD)
Length (mm)
    Min
    Max
    Median
    Mean (SD)
Width (mm)
    Min
    Max
    Median
    Mean (SD)

Or even like this: (with a width column too)

        Mass (g)                         Length (mm)      
Type    Min   Max   Median   Mean (SD)   Min   Max   Median   Mean (SD)
tct
tcx
tht
thx
tct

Here is an example of my data:

dat <- data.frame(
  "id" = c(01,02,03,04,05,06,07,08,09,10),
  "type" = c("tct", "tcx", "tht", "thx", "tct"),
  "mass.g" = c(0.03,0.01,0.04,0.06,0.07,0.03,0.03,0.01,0.04,0.02),
  "size.length" = c(8,6,5,6.5,5,5.5,6,7,4,3),
  "size.width" = c(2,4,3,4,5,6,3,4,2,1),
)

This is the code I am working on, taken from here.

library(qwraps2)

summary <-
  list("Mass (g)" =
         list(
              "Min" = ~ min(.data$mass.g),
              "Max" = ~ max(.data$mass.g),
              "Median" = ~ median(.data$mass.g)
              "Mean (SD)" = ~ qwraps2::mean_sd(.data$mass.g)),
       "Length (mm)" =
         list(
              "Min" = ~ min(.data$size.length),
              "Max" = ~ median(.data$size.length),
              "Median" = ~ max(.data$size.length),
              "Mean (SD)" = ~ qwraps2::mean_sd(.data$size.length)),
       "Width (mm)" =
         list(
              "Min" = ~ min(.data$size.width),
              "Max" = ~ median(.data$size.width),
              "Median" = ~ max(.data$size.width),
              "Mean (SD)" = ~ qwraps2::mean_sd(.data$size.width)
              ))
summary
by_type <- summary_table(dplyr::group_by(dat, type), summary)
by_type

But I keep getting the error message: "Error: x must be a formula"

My end goal is to complete a table that resembles the above and export it as an excel file or word doc.

Any help would be appreciated.


Solution

  • A minor correction when building the data set. I used dput() to get the structure of the object. This is a little more robust than using data.frame as the mode of each column is explicitly defined.

    dat <-
     structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), type = structure(c(1L,
     2L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 1L), class = "factor", .Label = c("tct",
     "tcx", "tht", "thx")), mass.g = c(0.03, 0.01, 0.04, 0.06, 0.07,
     0.03, 0.03, 0.01, 0.04, 0.02), size.length = c(8, 6, 5, 6.5,
     5, 5.5, 6, 7, 4, 3), size.width = c(2, 4, 3, 4, 5, 6, 3, 4, 2,
     1)), class = "data.frame", row.names = c(NA, -10L))
    

    loading the qwraps2 package and setting the markup language to “markdown”. Without this setting the default markup is LaTeX.

    library(qwraps2)
    options(qwraps2_markup = "markdown")
    

    The same summary as in the question posting, but with the missing comma added as noted in Ben’s comment.

    summary <-
      list("Mass (g)" =
           list(
                "Min" = ~ min(mass.g),
                "Max" = ~ max(mass.g),
                "Median" = ~ median(mass.g),
                "Mean (SD)" = ~ qwraps2::mean_sd(mass.g)),
           "Length (mm)" =
             list(
                  "Min" = ~ min(size.length),
                  "Max" = ~ median(size.length),
                  "Median" = ~ max(size.length),
                  "Mean (SD)" = ~ qwraps2::mean_sd(size.length)),
           "Width (mm)" =
             list(
                  "Min" = ~ min(size.width),
                  "Max" = ~ median(size.width),
                  "Median" = ~ max(size.width),
                  "Mean (SD)" = ~ qwraps2::mean_sd(size.width)
                  ))
    

    The summary table appears to render as expected:

    by_type <- summary_table(x = dat, summaries = summary, by = "type")
    by_type
    #> 
    #> 
    #> |                       |tct (N = 4)        |tcx (N = 2)        |tht (N = 2)        |thx (N = 2)        |
    #> |:----------------------|:------------------|:------------------|:------------------|:------------------|
    #> |**Mass (g)**           |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |
    #> |&nbsp;&nbsp; Min       |0.02               |0.01               |0.01               |0.04               |
    #> |&nbsp;&nbsp; Max       |0.07               |0.03               |0.04               |0.06               |
    #> |&nbsp;&nbsp; Median    |0.03               |0.02               |0.025              |0.05               |
    #> |&nbsp;&nbsp; Mean (SD) |0.04 &plusmn; 0.02 |0.02 &plusmn; 0.01 |0.03 &plusmn; 0.02 |0.05 &plusmn; 0.01 |
    #> |**Length (mm)**        |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |
    #> |&nbsp;&nbsp; Min       |3                  |6                  |5                  |4                  |
    #> |&nbsp;&nbsp; Max       |5.25               |6                  |6                  |5.25               |
    #> |&nbsp;&nbsp; Median    |8                  |6                  |7                  |6.5                |
    #> |&nbsp;&nbsp; Mean (SD) |5.38 &plusmn; 2.06 |6.00 &plusmn; 0.00 |6.00 &plusmn; 1.41 |5.25 &plusmn; 1.77 |
    #> |**Width (mm)**         |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |&nbsp;&nbsp;       |
    #> |&nbsp;&nbsp; Min       |1                  |3                  |3                  |2                  |
    #> |&nbsp;&nbsp; Max       |3.5                |3.5                |3.5                |3                  |
    #> |&nbsp;&nbsp; Median    |6                  |4                  |4                  |4                  |
    #> |&nbsp;&nbsp; Mean (SD) |3.50 &plusmn; 2.38 |3.50 &plusmn; 0.71 |3.50 &plusmn; 0.71 |3.00 &plusmn; 1.41 |
    

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

    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-09-15                  
    #> 
    #> ─ 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.4   2020-09-07 [1] CRAN (R 4.0.2)
    #>  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)
    #>  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)
    #>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
    #>  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)
    #>  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)
    #>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
    #>  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.4   2020-09-03 [1] CRAN (R 4.0.2)
    #>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
    #>  qwraps2     * 0.5.0   2020-09-14 [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.5.3   2020-09-09 [1] CRAN (R 4.0.2)
    #>  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)
    #>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
    #>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
    #>  xfun          0.17    2020-09-09 [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