输出两个 stargazer 表而不是一个

Outputting two stargazer tables instead of one

我在 github (https://github.com/labreumaia/longtable.stargazer/blob/master/longtable.stargazer.R) 上发现了这个结合了 stargazer 和 longtable 的漂亮代码:

longtable.stargazer = function(..., float = T, longtable.float = F, 
  longtable.head = T, filename = NULL){
  # Capturing stargazer to hack it
  require(stargazer)
  res = capture.output(
    stargazer(..., float = float)
  )
  # Changing tabulare environment for longtable
    res = gsub("tabular", "longtable", res)
  # removing floating environment
  if(float == T & longtable.float == F){
    res[grep("table", res)[1]] = res[grep("longtable", res)[1]]
    # Removing extra longtable commands
    res = res[-grep("longtable", res)[2]]
    res = res[-length(res)]
  }
  # Adding page headings
  if(longtable.head == T){
    res = c(res[1:which(res == "\hline \\[-1.8ex] ")[1] - 1], "\endhead", res[which(res == "\hline \\[-1.8ex] ")[1]:length(res)])
  }
  # Exporting
  cat(res, sep = "\n")
  # Exporting
  if(!is.null(filename)){
    cat(res, file = filename, sep = "\n")
    # Message
    cat(paste("\nLaTeX output printed to", filename, "\n", sep = " ", 
      collapse = ""))
  }else{
    cat(res, sep = "\n")
  }
}

但是当我对汽车数据进行以下回归分析时:

ols <- lm(speed ~ dist, data = cars)

longtable.stargazer(ols)

我最终输出了两个表。为什么会这样?

这是我得到的输出:

% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, May 05, 2021 - 20:22:44
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{longtable}{@{\extracolsep{5pt}}lc} 
\[-1.8ex]\hline 
\endhead
\hline \[-1.8ex] 
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \ 
\cline{2-2} 
\[-1.8ex] & speed \ 
\hline \[-1.8ex] 
 dist & 0.166$^{***}$ \ 
  & (0.017) \ 
  & \ 
 Constant & 8.284$^{***}$ \ 
  & (0.874) \ 
  & \ 
\hline \[-1.8ex] 
Observations & 50 \ 
R$^{2}$ & 0.651 \ 
Adjusted R$^{2}$ & 0.644 \ 
Residual Std. Error & 3.156 (df = 48) \ 
F Statistic & 89.567$^{***}$ (df = 1; 48) \ 
\hline 
\hline \[-1.8ex] 
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<[=12=].1; $^{**}$p$<[=12=].05; $^{***}$p$<[=12=].01} \ 
\end{longtable} 
\end{table} 

% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, May 05, 2021 - 20:22:44
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{longtable}{@{\extracolsep{5pt}}lc} 
\[-1.8ex]\hline 
\endhead
\hline \[-1.8ex] 
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \ 
\cline{2-2} 
\[-1.8ex] & speed \ 
\hline \[-1.8ex] 
 dist & 0.166$^{***}$ \ 
  & (0.017) \ 
  & \ 
 Constant & 8.284$^{***}$ \ 
  & (0.874) \ 
  & \ 
\hline \[-1.8ex] 
Observations & 50 \ 
R$^{2}$ & 0.651 \ 
Adjusted R$^{2}$ & 0.644 \ 
Residual Std. Error & 3.156 (df = 48) \ 
F Statistic & 89.567$^{***}$ (df = 1; 48) \ 
\hline 
\hline \[-1.8ex] 
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<[=12=].1; $^{**}$p$<[=12=].05; $^{***}$p$<[=12=].01} \ 
\end{longtable} 
\end{table} 

有一个松散的 cat() 调用,紧接着 if/else 中的另一个 cat() 使输出加倍。注释掉那一行:

longtable.stargazer = function(..., float = T, longtable.float = F, 
  longtable.head = T, filename = NULL){
  # Capturing stargazer to hack it
  require(stargazer)
  res = capture.output(
    stargazer(..., float = float)
  )
  # Changing tabulare environment for longtable
    res = gsub("tabular", "longtable", res)
  # removing floating environment
  if(float == T & longtable.float == F){
    res[grep("table", res)[1]] = res[grep("longtable", res)[1]]
    # Removing extra longtable commands
    res = res[-grep("longtable", res)[2]]
    res = res[-length(res)]
  }
  # Adding page headings
  if(longtable.head == T){
    res = c(res[1:which(res == "\hline \\[-1.8ex] ")[1] - 1], "\endhead", res[which(res == "\hline \\[-1.8ex] ")[1]:length(res)])
  }
  # Exporting
  # cat(res, sep = "\n") <---- comment out this line
  # Exporting
  if(!is.null(filename)){
    cat(res, file = filename, sep = "\n")
    # Message
    cat(paste("\nLaTeX output printed to", filename, "\n", sep = " ", 
      collapse = ""))
  }else{
    cat(res, sep = "\n")
  }
}
ols <- lm(speed ~ dist, data = cars)
longtable.stargazer(ols)