How to save the output of the expss package?

258 Views Asked by At

I use the expss package to produce summary tables of the mtcar data.

library(expss)
data(mtcars)

mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(total(), vs) %>% 
    tab_rows(am) %>% 
    tab_stat_cpct(total_row_position = "above",
                  total_label = c("number of cases", "row %"),
                  total_statistic = c("u_cases", "u_rpct")) %>% 
    tab_pivot()

I get such output: car output

Now I would like to save the output in a html, pdf or jpeg file. Unfortunately encooperating save.image before or within the loop does not work. There must be an easy solution to this? I also tried to export from the viewer somehow and failed as well.

Further is there a possibility to display the number of cases of the factor (i.e. cylinders) per different engine and its overall proportion in one row (e.g. n case (xy%) ?)

1

There are 1 best solutions below

1
On

To save table as html you can add htmlTable function at the end of your sequence and then save HTML code:

library(expss)
data(mtcars)

mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(total(), vs) %>% 
    tab_rows(am) %>% 
    tab_stat_cpct(total_row_position = "above",
                  total_label = c("number of cases", "row %"),
                  total_statistic = c("u_cases", "u_rpct")) %>% 
    tab_pivot() %>% 
    htmlTable() %>% 
    writeLines("my_table.html")

You can produce cases and table percent in one row with the following code:

library(expss)
data(mtcars)

mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(total(), vs) %>% 
    tab_rows(am) %>% 
    tab_stat_cases(total_row_position = "none") %>% 
    tab_stat_tpct(total_row_position = "none") %>% 
    tab_pivot(stat_position = "inside_columns")

# |    |    |     |    | #Total |       | vs |       |    |       |
# |    |    |     |    |        |       |  0 |       |  1 |       |
# | -- | -- | --- | -- | ------ | ----- | -- | ----- | -- | ----- |
# | am |  0 | cyl |  4 |      3 | 15.79 |    |       |  3 | 15.79 |
# |    |    |     |  6 |      4 | 21.05 |    |       |  4 | 21.05 |
# |    |    |     |  8 |     12 | 63.16 | 12 | 63.16 |    |       |
# |    |  1 | cyl |  4 |      8 | 61.54 |  1 |  7.69 |  7 | 53.85 |
# |    |    |     |  6 |      3 | 23.08 |  3 | 23.08 |    |       |
# |    |    |     |  8 |      2 | 15.38 |  2 | 15.38 |    |       |

By now there is no option to put statistics in brackets in the same cell.