使用 xtable - ftable - knitr 打印表格时选项不一致
Inconsistent options when printing tables with xtable - ftable - knitr
我有一些多维数据(调查数据:一些人互相回答问题),我想用 knitr 输出一个 latex table。一切都按预期工作,除了 table 很长,所以我想使用格式选项 longtable
自动断开页面底部的 table 并继续下一个.由于某种原因这不起作用,该选项被忽略:代码输出 "tex table" 但采用默认的 "table" 格式。但是,不会忽略其他选项(例如 rotate.colnames
)。
我首先制作一个 ftable
并使用 xtableFtable
(包 xtable
的一部分)将其转换为 xtable。然后我使用 print.xtableFtable
函数以乳胶格式输出实际的 table 。
参考 print.xtableFtable 上的文档,然后我使用选项 tabular.environment = getOption("xtable.tabular.environment", "longtable")
。但是,此选项将被忽略(也没有错误或警告消息)并打印 "normal" table。使用缩写 tabular.environment = "longtable"
也会被忽略。
奇怪的是,选项 rotate.colnames = TRUE
没有被忽略,但我不明白为什么。它可能与 xtableFtable
函数有关,因为正常的二维 table 是使用 "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")
我非常欢迎任何建议、解决方案或其他方法来完成这项工作
不是 关于函数为何忽略某些选项的答案,但感谢 user20650 将我指向较早的 question/answer 建议设置全局选项而不是在函数中传递它。
这现在有效:
# 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 代码时会出错,但这完全是另一个问题)
我有一些多维数据(调查数据:一些人互相回答问题),我想用 knitr 输出一个 latex table。一切都按预期工作,除了 table 很长,所以我想使用格式选项 longtable
自动断开页面底部的 table 并继续下一个.由于某种原因这不起作用,该选项被忽略:代码输出 "tex table" 但采用默认的 "table" 格式。但是,不会忽略其他选项(例如 rotate.colnames
)。
我首先制作一个 ftable
并使用 xtableFtable
(包 xtable
的一部分)将其转换为 xtable。然后我使用 print.xtableFtable
函数以乳胶格式输出实际的 table 。
参考 print.xtableFtable 上的文档,然后我使用选项 tabular.environment = getOption("xtable.tabular.environment", "longtable")
。但是,此选项将被忽略(也没有错误或警告消息)并打印 "normal" table。使用缩写 tabular.environment = "longtable"
也会被忽略。
奇怪的是,选项 rotate.colnames = TRUE
没有被忽略,但我不明白为什么。它可能与 xtableFtable
函数有关,因为正常的二维 table 是使用 "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")
我非常欢迎任何建议、解决方案或其他方法来完成这项工作
不是 关于函数为何忽略某些选项的答案,但感谢 user20650 将我指向较早的 question/answer 建议设置全局选项而不是在函数中传递它。
这现在有效:
# 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 代码时会出错,但这完全是另一个问题)