1

我在 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$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.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$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{longtable} 
\end{table} 
4

1 回答 1

1

有一个松散的cat()调用,然后是另一个cat()内部if/else,它的输出加倍。注释掉那一行:

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)

在此处输入图像描述

于 2021-05-06T01:39:37.943 回答