使用xtable打印表时不一致的选项 - ftable - knitr

时间:2018-01-16 22:37:06

标签: r knitr xtable

我有一些多维数据(调查数据:有些人回答彼此的问题),我想用knitr输出一个乳胶表。一切都按预期工作,除了表格很长,所以我想使用格式化选项longtable自动打破页面底部的表格并继续下一个表格。这由于某种原因不起作用,该选项被忽略:代码输出" tex表"但在默认的"表"格式。但是,其他选项不会被忽略(例如rotate.colnames)。

我首先制作ftable并使用xtableFtable(包xtable的一部分)将其转换为xtable。然后我使用print.xtableFtable函数以乳胶格式输出实际表。

参考print.xtableFtable上的文档,然后使用选项tabular.environment = getOption("xtable.tabular.environment", "longtable")。但是,忽略此选项(也没有错误或警告消息)和"正常"表打印。使用短格式tabular.environment = "longtable"也会被忽略。

奇怪的是,选项rotate.colnames = TRUE不会被忽略,但我不明白为什么。它可能与xtableFtable函数有关,因为正常的二维表打印了" longtable"选项,但我不确切知道。

# 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")

我非常欢迎任何建议,解决方案或其他方式来完成这项工作

1 个答案:

答案 0 :(得分:0)

不是 回答为什么函数会忽略某些选项,但感谢user20650指出我之前的问题/答案,建议设置全局选项而不是传入功能。

现在确实有效:

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

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

(出于某种原因,选项rotate.columns = TRUE现在在编译tex代码时出错,但这是另一个问题)