qwraps2::summary_table: Error in formula.default(object, env = baseenv()) : invalid formula

326 Views Asked by At

I want to create a table with {qwraps2}. Therefore I followed one of the basic tutorials in the web (https://www.r-bloggers.com/2016/12/baseline-characteristics-tables-with-qwraps2-2/). I guess it is quite simple to produce such a table, but somehow I got stock, when using the 'summary_table' function --> "Error in formula.default(object, env = baseenv()) : invalid formula". What I tried / found: 1.) I already read some of the linked answer to this response, but it seems everytime a quite unique solution to a general problem. 2.) It is surely no problem of '~' & lm-/glm-function. 3.) I tried other summary functions offered by qwraps2 and searched for typos or misleading signs (to exclude kind of problems like in 2.)..)

This is my code:

#### begin

# created combined variable
tbl4 <- tbl4 %>% 
  mutate(perc_total = (tbl4$image == 1 | tbl4$image == 2))

# table input
img_type <-
  list("Title" =
       list((tbl4$house)),
      "Title 01" =
       list("CT, n (%)"     = ~ perc_n(image == "1"),
            "MR, n (%)"     = ~ perc_n(image == "2")),
       "Title 02" = 
       list("% of total"    = ~ perc_n(perc_total),
            "%CT"          = ~ perc_n(ct == "1"),
            "%MR"          = ~ perc_n(mr == "1"))
       )

table_img_type <- summary_table(tbl4, img_type) 
#### end

Note: all variables are factors & all NA are excluded.

As I am an absolute beginner & as I am sitting here since a while, I'll be very thankful for any sort of help... Thanks in advance!

1

There are 1 best solutions below

0
Peter On

The issue is with the "Title" element of img_type. list((tbl4$house)) is not a function and thus the error.

My guess at what you are looking for is to label columns. If that is the case then try this:

img_type <-
  list(
      "Title 01" =
       list("CT, n (%)"     = ~ perc_n(image == "1"),
            "MR, n (%)"     = ~ perc_n(image == "2")),
       "Title 02" = 
       list("% of total"    = ~ perc_n(perc_total),
            "%CT"          = ~ perc_n(ct == "1"),
            "%MR"          = ~ perc_n(mr == "1"))
       )

summary_table(tbl4, img_type, by = "house")