如何抑制输出文件中的stargazer()信息

时间:2022-03-16 18:23:08

I'm using the stargazer package to output an R dataframe as latex code:

我使用stargazer包输出一个R dataframe作为乳胶代码:

library(stargazer)

stargazer(mtcars)

And this is the output

这是输出

% Table created by StarGazer v.3.0.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Thu, May 09, 2013 - 16:14:28
\begin{table}[htb] \centering 
  \caption{} 
  \label{} 
\footnotesize 

\begin{tabular}{@{\extracolsep{5pt}}l c c c c c } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
mpg & 32 & 20.091 & 6.027 & 10.400 & 33.900 \\ 
cyl & 32 & 6.188 & 1.786 & 4 & 8 \\ 
disp & 32 & 230.722 & 123.939 & 71.100 & 472.000 \\ 
hp & 32 & 146.688 & 68.563 & 52 & 335 \\ 
drat & 32 & 3.597 & 0.535 & 2.760 & 4.930 \\ 
wt & 32 & 3.217 & 0.978 & 1.513 & 5.424 \\ 
qsec & 32 & 17.849 & 1.787 & 14.500 & 22.900 \\ 
vs & 32 & 0.438 & 0.504 & 0 & 1 \\ 
am & 32 & 0.406 & 0.499 & 0 & 1 \\ 
gear & 32 & 3.688 & 0.738 & 3 & 5 \\ 
carb & 32 & 2.812 & 1.615 & 1 & 8 \\ 
\hline \\[-1.8ex] 
\normalsize 
\end{tabular} 
\end{table} 

Note the output includes two lines of latex comments (top two lines, beginning %). How can I stop the top two lines (beginning %) from being outputted?

注意,输出包含两行乳胶注释(前两行,起始%)。我怎样才能阻止前两行(开始%)被输出?

A similar question, but concerning xtable() was asked here: Using table caption on R markdown file using knitr to use in pandoc to convert to pdf

这里提出了一个类似的问题,但与xtable()有关:使用R markdown文件上的表标题,使用knitr在pandoc中将表标题转换为pdf

2 个解决方案

#1


14  

From version 4.0 onwards (available on CRAN now), you can run stargazer with the argument header=FALSE to omit the initial comments in your Latex code ouput.

从4.0版本开始(现在可以在CRAN上使用),您可以使用参数标头=FALSE运行stargazer,以忽略Latex代码输出中的初始注释。

#2


6  

One could just modify the output directly

可以直接修改输出

mod_stargazer <- function(...){
  output <- capture.output(stargazer(...))
  # The first three lines are the ones we want to remove...
  output <- output[4:length(output)]
  # cat out the results - this is essentially just what stargazer does too
  cat(paste(output, collapse = "\n"), "\n")
}

which gives

这给了

> mod_stargazer(mtcars)
\begin{table}[htb] \centering 
  \caption{} 
  \label{} 
\footnotesize 

\begin{tabular}{@{\extracolsep{5pt}}l c c c c c } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
mpg & 32 & 20.091 & 6.027 & 10.400 & 33.900 \\ 
cyl & 32 & 6.188 & 1.786 & 4 & 8 \\ 
disp & 32 & 230.722 & 123.939 & 71.100 & 472.000 \\ 
hp & 32 & 146.688 & 68.563 & 52 & 335 \\ 
drat & 32 & 3.597 & 0.535 & 2.760 & 4.930 \\ 
wt & 32 & 3.217 & 0.978 & 1.513 & 5.424 \\ 
qsec & 32 & 17.849 & 1.787 & 14.500 & 22.900 \\ 
vs & 32 & 0.438 & 0.504 & 0 & 1 \\ 
am & 32 & 0.406 & 0.499 & 0 & 1 \\ 
gear & 32 & 3.688 & 0.738 & 3 & 5 \\ 
carb & 32 & 2.812 & 1.615 & 1 & 8 \\ 
\hline \\[-1.8ex] 
\normalsize 
\end{tabular} 
\end{table}  

#1


14  

From version 4.0 onwards (available on CRAN now), you can run stargazer with the argument header=FALSE to omit the initial comments in your Latex code ouput.

从4.0版本开始(现在可以在CRAN上使用),您可以使用参数标头=FALSE运行stargazer,以忽略Latex代码输出中的初始注释。

#2


6  

One could just modify the output directly

可以直接修改输出

mod_stargazer <- function(...){
  output <- capture.output(stargazer(...))
  # The first three lines are the ones we want to remove...
  output <- output[4:length(output)]
  # cat out the results - this is essentially just what stargazer does too
  cat(paste(output, collapse = "\n"), "\n")
}

which gives

这给了

> mod_stargazer(mtcars)
\begin{table}[htb] \centering 
  \caption{} 
  \label{} 
\footnotesize 

\begin{tabular}{@{\extracolsep{5pt}}l c c c c c } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
mpg & 32 & 20.091 & 6.027 & 10.400 & 33.900 \\ 
cyl & 32 & 6.188 & 1.786 & 4 & 8 \\ 
disp & 32 & 230.722 & 123.939 & 71.100 & 472.000 \\ 
hp & 32 & 146.688 & 68.563 & 52 & 335 \\ 
drat & 32 & 3.597 & 0.535 & 2.760 & 4.930 \\ 
wt & 32 & 3.217 & 0.978 & 1.513 & 5.424 \\ 
qsec & 32 & 17.849 & 1.787 & 14.500 & 22.900 \\ 
vs & 32 & 0.438 & 0.504 & 0 & 1 \\ 
am & 32 & 0.406 & 0.499 & 0 & 1 \\ 
gear & 32 & 3.688 & 0.738 & 3 & 5 \\ 
carb & 32 & 2.812 & 1.615 & 1 & 8 \\ 
\hline \\[-1.8ex] 
\normalsize 
\end{tabular} 
\end{table}