我在自动rmarkdown pdf文档中使用stargazer包来制作漂亮的表格。默认情况下,Stargazer将其表格放在页面的中心。我如何让stargazer生成乳胶代码,使表格左对齐?
这是我的意思的一个例子:
library(stargazer)
data_object <- structure(list(test = structure(c(1L, 1L, 2L, 2L), .Label = c("test1", "test2"), class = "factor"), test2 = structure(1:4, .Label = c("1", "2", "3", "4"), class = "factor")), .Names = c("test", "test2"), row.names = c(NA, -4L), class = "data.frame")
stargazer(data_object,title="table test",summary=FALSE,rownames=FALSE,type="latex",header=FALSE)
它产生的代码是:
\begin{table}[!htbp] \centering
\caption{table test}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} cc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
test & test2 \\
\hline \\[-1.8ex]
test1 & 1 \\
test1 & 2 \\
test2 & 3 \\
test2 & 4 \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
请注意\centering
。如何在不改变乳胶代码本身的情况下改变它?
答案 0 :(得分:2)
\centering
似乎是hard coded into the function。您可以做的是使用\centering
删除sub
(例如sub(" \\\\centering", "", out)
)。
这是我使用的块。我使用capture.output
来阻止stargazer
输出我认为的中间结果。
<<results = "asis">>=
library(stargazer)
data_object <- structure(list(test = structure(c(1L, 1L, 2L, 2L), .Label = c("test1", "test2"), class = "factor"), test2 = structure(1:4, .Label = c("1", "2", "3", "4"), class = "factor")), .Names = c("test", "test2"), row.names = c(NA, -4L), class = "data.frame")
out <- capture.output(stargazer(data_object,title="table test",summary=FALSE,rownames=FALSE,type="latex",header=FALSE))
out <- sub(" \\\\centering", "", out)
cat(out)
@