1
votes

I am trying to use R markdown to create a report that contains 3-way contingency tables. I am able to create the tables quite easily using ftable(), but they are not formatted well for printing in markdown.

The output has to be a DOCX document. Therefore, anything that is designed to format for HTML or LaTeX will not be useful to me.

The kable() function in knitr does not work with ftables, and many other options will simply print it as a data frame. When converting an ftable to a data frame, it messes up how it is laid out, so those are not helpful, either.

I created a reproducible example of a 3-way table created using ftable that should be roughly the size of the tables I need to output:

library(tidyverse)
# this data set is included in dplyr
data("starwars")
data=starwars
#the next couple of lines are just me trimming down the information to be used in the table
data=data %>% filter(species=="Human" | species=="Droid" | species=="Zabrak" | species=="Mirialan" | species=="Wookiee")
tabledata=data[,c(8:10)]
#here's the actual table
table1 = ftable(tabledata,col.vars=c(3,1))
table1

I'm hoping for a fairly simple solution, if possible (maybe a package that does it for me, or something). This report will contain many tables, and so doing something too complex will take too long (also, I've only begun using R recently).

Thanks in advance for any help y'all can provide!

2

2 Answers

1
votes

Are you open to slightly different representations of the ftable? For example, here is a solution using flextable. This renders very nicely in Word... I used flextable to build the tables in a 500-page document last year.

library(flextable)
library(dplyr)
library(magrittr)

table1 = ftable(tabledata,col.vars=c(3,1))

data.frame(table1) %>% spread(species, Freq) %>% 
     regulartable %>% merge_v(j = ~homeworld) %>% autofit()

enter image description here

1
votes

You can do more beautiful things with htmlTable (for example in Tableau-like grouped table in R for markdown) but here is a quick and dirty solution that may suffice in R Markdown, using format_html from memisc package:

```{r test, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(tidyverse)
library(memisc)
library(knitr)

tabledata <- structure(list(gender = c("male", NA, NA, "male", "female", "male", 
"female", NA, "male", "male", "male", "male", "male", "male", 
"male", "male", "male", "male", "none", "male", "male", "female", 
"male", "male", "male", "female", "male", "male", "male", "male", 
"female", "male", "female", "female", "female", "male", "male", 
"male", "female", "male", "male", "male", "female", "male", "none", 
"female"), homeworld = c("Tatooine", "Tatooine", "Naboo", "Tatooine", 
"Alderaan", "Tatooine", "Tatooine", "Tatooine", "Tatooine", "Stewjon", 
"Tatooine", "Eriadu", "Kashyyyk", "Corellia", "Corellia", "Bestine IV", 
"Naboo", "Kamino", NA, "Socorro", "Bespin", "Chandrila", NA, 
NA, "Coruscant", "Tatooine", "Dathomir", "Haruun Kal", "Iridonia", 
"Naboo", "Naboo", "Tatooine", "Mirial", "Mirial", "Naboo", "Serenno", 
"Alderaan", "Concord Dawn", "Coruscant", "Kashyyyk", "Alderaan", 
NA, NA, NA, NA, "Naboo"), species = c("Human", "Droid", "Droid", 
"Human", "Human", "Human", "Human", "Droid", "Human", "Human", 
"Human", "Human", "Wookiee", "Human", "Human", "Human", "Human", 
"Human", "Droid", "Human", "Human", "Human", "Human", "Human", 
"Human", "Human", "Zabrak", "Human", "Zabrak", "Human", "Human", 
"Human", "Mirialan", "Mirialan", "Human", "Human", "Human", "Human", 
"Human", "Wookiee", "Human", "Human", "Human", "Human", "Droid", 
"Human")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-46L))

table1 = ftable(tabledata,col.vars=c(3,1))

knitr::asis_output(htmltools::htmlPreserve(
   format_html(table1,
               show.titles=TRUE,
               digits=0,
               format="f",
               toprule=2,midrule=1,bottomrule=2,
               split.dec=TRUE,
               style = ftable_format_stdstyle,
               margin="2ex auto", 
               varontop,varinfront)
))
```

It looks like this:

enter image description here