请考虑以下MWE
library(xtable)
DF <- as.data.frame(UCBAdmissions)
print(xtable(DF, align="p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}"), sanitize.text.function = function(x){x},
table.placement="!htp", include.rownames=FALSE,
tabular.environment='longtable',floating=FALSE)
我想像这样设置
longtable
的对齐方式\begin{longtable}{p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}}
仍然当我尝试将参数传递给
xtable
对象时,我得到了Warning message:
In .alignStringToVector(value) : Nonstandard alignments in align string
Error in print(xtable(DF, align = "p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}"), :
error in evaluating the argument 'x' in selecting a method for function 'print': Error in `align<-.xtable`(`*tmp*`, value = "p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}") :
"align" must have length equal to 5 ( ncol(x) + 1 )
我知道我应该为第5列添加对齐方式(如何?),但我也不了解错误消息。我应该对琴弦进行消毒吗?
最佳答案
我无法对此进行测试,但是我认为您需要对字符串中的反斜杠应用标准R转义,删除多余的“ \”并添加缺少的“竖线”(|
)。然后align<-
成功,但仅显示警告:
xtb <- xtable(DF,
table.placement="!htp", include.rownames=FALSE,
tabular.environment='longtable',floating=FALSE)
align(xtb) <- "p{0.4\\textwidth}|p{0.15\\textwidth}|p{0.15\\textwidth}| p{0.15\\textwidth}"
#Warning message:
#In .alignStringToVector(value) : Nonstandard alignments in align string
print(xtb)
要么:
xtb <- xtable(DF, type="latex",
table.placement="!htp", include.rownames=FALSE,
tabular.environment='longtable',floating=FALSE, align= c("p{0.15\\textwidth}",
"p{0.4\\textwidth}", "p{0.15\\textwidth}|", "p{0.15\\textwidth}", "p{0.15\\textwidth}"
))