如何在 type="latex" 时将 stargazer table 左对齐?

how to align stargazer table to the left when type="latex"?

我在我的自动 rmarkdown pdf 文档中使用 stargazer 包来制作漂亮的 tables。默认情况下,Stargazer 将其 table 放置在页面中央。如何让 stargazer 生成将 table 左对齐的乳胶代码?

这是我的意思的一个例子:

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。我怎样才能改变它而不必改变乳胶代码本身?

看起来 \centeringhard coded into the function。您可以做的是使用 sub 删除 \centering(例如 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)
@