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

709 Views Asked by At

I am having trouble creating building the list-of-lists to pass to the summaries argument of summary_table. When I go to use the summary_table function, it returns

"Error: x must be a formula"

summary_test <- list("Gender" = 
     list("Female" = ~ qwraps2::n_perc0(.mydata$sex == "F"),
           "Male" = ~ qwraps2::n_perc0(.mydata$sex == "M")),
  "Age" =
  list("Mean" = ~ qwraps2::mean_sd(.mydata$age, denote_sd = "paren")),
 "Comorbidities" =
   list("HIV Positive" == ~ qwraps2::n_perc0(.mydata$hiv == 1),
        "Type 2 Diabetes" == ~ qwraps2::n_perc0(.mydata$diabetes == 1)))

whole <- summary_table(mydata, summary_test)
3

There are 3 best solutions below

0
Wil On

can't reproduce without your data, but it looks like you wrote == instead of = in the last list. Try:

summary_test <- list("Gender" = 
                   list("Female" = ~ qwraps2::n_perc0(.mydata$sex == "F"),
                        "Male" = ~ qwraps2::n_perc0(.mydata$sex == "M")),
                 "Age" =
                   list("Mean" = ~ qwraps2::mean_sd(.mydata$age, denote_sd = "paren"),
                        "Comorbidities" =
                          list("HIV Positive" = ~ qwraps2::n_perc0(.mydata$hiv == 1),
                               "Type 2 Diabetes" = ~ qwraps2::n_perc0(.mydata$diabetes == 1)))

whole <- summary_table(mydata, summary_test)
0
epi_stats On

In the end this worked:

summary1 <- list("Age" = 
         list(
           "Mean" = ~ qwraps2::mean_sd(.data$age, digits=1)),
   "Gender" =
       list(
         "Male" = ~ qwraps2::n_perc(.data$sex == "M", digits = 1),
         "Female" = ~ qwraps2::n_perc(.data$sex == "F", digits = 1)),
   "Comorbidities" =
     list(
       "Type 2 Diabetes" = ~ qwraps2::n_perc(.data$diabetes == "1", digits = 1),
       "Past History of PTB" = ~ qwraps2::n_perc(.data$past.ptb == "1", digits = 1),
       "HIV" = ~ qwraps2::n_perc(.data$hiv == "1", digits = 1)
     ))
0
Peter On

Summaries are to be lists of lists of formulae, that is, the highest level object is a list with each element being a list. The elements of these lower level lists are all to be formulae. The provided summary:

summary_test <- list("Gender" = 
                       list("Female" = ~ qwraps2::n_perc0(.mydata$sex == "F"),
                            "Male"   = ~ qwraps2::n_perc0(.mydata$sex == "M")),
                     "Age" =
                       list("Mean" = ~ qwraps2::mean_sd(.mydata$age, denote_sd = "paren")),
                     "Comorbidities" =
                       list("HIV Positive"    == ~ qwraps2::n_perc0(.mydata$hiv == 1),
                            "Type 2 Diabetes" == ~ qwraps2::n_perc0(.mydata$diabetes == 1)))

has == in the definition for "Comorbidities" and thus logical statements, not formulae.

str(summary_test) 
#> List of 3
#>  $ Gender       :List of 2
#>   ..$ Female:Class 'formula'  language ~qwraps2::n_perc0(.mydata$sex == "F")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..$ Male  :Class 'formula'  language ~qwraps2::n_perc0(.mydata$sex == "M")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Age          :List of 1
#>   ..$ Mean:Class 'formula'  language ~qwraps2::mean_sd(.mydata$age, denote_sd = "paren")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Comorbidities:List of 2
#>   ..$ : logi FALSE
#>   ..$ : logi FALSE

Also, the .mydata needs to be replaced with the correct tidyverse data pronoun .data. The correct syntax is:

summary_test <- list("Gender" = 
                       list("Female" = ~ qwraps2::n_perc0(.data$sex == "F"),
                            "Male"   = ~ qwraps2::n_perc0(.data$sex == "M")),
                     "Age" =
                       list("Mean" = ~ qwraps2::mean_sd(.data$age, denote_sd = "paren")),
                     "Comorbidities" =
                       list("HIV Positive"    = ~ qwraps2::n_perc0(.data$hiv == 1),
                            "Type 2 Diabetes" = ~ qwraps2::n_perc0(.data$diabetes == 1)))

str(summary_test)
#> List of 3
#>  $ Gender       :List of 2
#>   ..$ Female:Class 'formula'  language ~qwraps2::n_perc0(.data$sex == "F")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..$ Male  :Class 'formula'  language ~qwraps2::n_perc0(.data$sex == "M")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Age          :List of 1
#>   ..$ Mean:Class 'formula'  language ~qwraps2::mean_sd(.data$age, denote_sd = "paren")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Comorbidities:List of 2
#>   ..$ HIV Positive   :Class 'formula'  language ~qwraps2::n_perc0(.data$hiv == 1)
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..$ Type 2 Diabetes:Class 'formula'  language ~qwraps2::n_perc0(.data$diabetes == 1)
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>

Created on 2019-11-14 by the reprex package (v0.3.0)

UPDATE As of qwraps2 v0.5.0 the use of the .data pronoun is no longer needed.