Export contTables

Hi, I am producing some very basic contTables and want to be able to put them into a document without having to screenshot but so that they still format properly. I doesn't need to be flashy at all I just want to export it!

My code for the tables looks like this:

pcRow =TRUE,

It produces this table
Contingency Tables
euRefpastVote No, did not vote Yes, voted Total
Stay/remain in the EU Observed 161 1983 2144
Expected 185 1959 2144
% within row 7.5 92.5 100.0
Leave the EU Observed 163 1445 1608
Expected 139 1469 1608
% within row 10.1 89.9 100.0
Total Observed 324 3428 3752
Expected 324 3428 3752
% within row 8.6 91.4 100.0
χ² Tests
Value df p
χ² 8.04 1 0.005
N 3752

Thanks in advance!

Hi @WWhipple,
We don't have your data so can't reproduce your output. However, I assume you are using
the jmv package. Also, you could just use cut-and-paste from the console window to get the tables into, say, a Word document but that won't be very easy to handle and will need a fixed-spaced font to look OK. A better way is to write the tables directly to a Word document; something like this (using the example from help("contTables"):


dat <- as.data.frame(HairEyeColor)

out <- contTables(formula = Freq ~ Hair:Eye, dat)

freqs <- as.data.frame(out$freqs)	  # the table of proportions
chisq <- as.data.frame(out$chiSq)	  # the table of X² test results


my_doc <- read_docx() # Initialise new Word document object

# Add headings and the two saved tables
my_doc <- my_doc %>% 
  body_add_par("Contingency Table", style = "heading 1") %>% 
  body_add_table(freqs, style = "table_template") %>%
  body_add_par("Chi-squared Results", style = "heading 1") %>% 
  body_add_table(chisq, style = "table_template")

# Write the object to Word document file.
print(my_doc, target = "~/first_example.docx")

# Open in Word to check contents.

If this is to be a regular job for you, I suggest you learn Rmarkdown to make it much easier and reproducible.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.