1
votes

I have some multidimensional data (survey data: some people answering questions about each other) and I want to output a latex table using knitr. Everything works as expected, except that the table is quite long and so I want to use the formatting option longtable to automatically break the table at the bottom of the page and continue on the next. This is for some reason not working, the option is ignored: the code outputs a "tex table" but in the default "table" format. Other options are not ignored, however (e.g. rotate.colnames).

I first make a ftable and convert this to an xtable using xtableFtable (part of the package xtable). Then I use the print.xtableFtable function to output the actual table in latex formatting.

Referencing the documentation on print.xtableFtable, I then use the option tabular.environment = getOption("xtable.tabular.environment", "longtable"). However, this option is ignored (also no error or warning message) and a "normal" table is printed. Using the short form tabular.environment = "longtable" also is ignored.

Weird thing is, the option rotate.colnames = TRUE is not ignored, but I don't understand why. It might have to do with the xtableFtable function because a normal 2-dimensional table is printed with the "longtable" option, but I don't know exactly.

# sample data - 3 people asked how often they agree with the others
dat <- data.frame("interviewee" = c(1, 1, 2, 2, 3, 3), "subject" = c(2, 2, 3, 3, 1, 1, 1, 3, 1, 2, 2, 2), "answer" = c("agrees", "agrees", "agrees", "disagrees", "disagrees", "disagrees", "agrees", "agrees", "disagrees", "disagrees", "disagrees", "agrees"))

# using ftable to see frequencies how often people agree and disagree with each other
# (this is also how I would like the latex table to look)
f.dat <- ftable(dat$interviewee, dat$answer, dat$subject)

# converting to xtable 
x.f.table <- xtableFtable(f.dat)

# printing to .tex format
print.xtableFtable(x.f.table)

# previous line works as expected, except that the table is spanning multiple pages so I want to use the option "longtable" 
# the next lines don't work (as in, the environment option is ignored)
print.xtableFtable(x.f.table, tabular.environment = getOption("xtable.tabular.environment", "longtable"))
print.xtableFtable(x.f.table, tabular.environment = "longtable") 

# this however does work: the column names are rotated
print.xtableFtable(x.f.table, rotate.colnames = TRUE)

# using a normal table, so without ftable, gives output using the option longtable
simple.table = table(dat$interviewee, dat$answers)
print(simple.table, tabular.environment = "longtable")

I would very much welcome any suggestions, solutions or otherwise ways to make this work

1
maybe relevant - so try setting the option globallyuser20650
I'm not an expert, but the examples of longtables that I found look like it is instead of both the table and tabular environments, so you might need to deal with that as well.Gregor Thomas

1 Answers

0
votes

Not really the answer as to why the function ignores some of the options but thanks to user20650 pointing me to an earlier question/answer suggesting to set global options instead of passing it in the function.

This does work now:

# first set the global options
options(xtable.tabular.environment = "longtable")

# continuing with the data from the question
print.xtableFtable(x.f.table)

(for some reason the option rotate.columns = TRUE now gives an error when compiling the tex code but that is another issue altogether)