'R package stargazer produces two table outputs instead of one

I'm currently struggling with r markdown, knitr, and, the stargazer package. More specifically, I want to produce the output of a logistic regression:

{r table1, include = TRUE, echo = FALSE, results='asis', message = FALSE}

testmodel <- glm(hra_target_bin ~ size_log + as.factor(private) + as.factor(MNE) + org_age, data = cdclean, family = binomial)
realmodel <- glm(hra_target_bin ~ size_log + as.factor(private) + as.factor(MNE) + org_age + strat_int + devolvement + s5v2, data = cdclean, family = binomial)
        
stargazer(testmodel, realmodel, type = "latex",  title = "Logistic regression of
organizational characteristics on the adoption of HR analytics", no.space = TRUE,
dep.var.labels="Adoption of HR analytics", covariate.labels = c("Size (log)","Private 
sector", "MNE", "Org. age", "Strategic integration", "Devolvement of HRM", "Trade unions' influence",
"Intercept"), ci = TRUE, odd.ratio = TRUE, header = FALSE)

However, the package keeps producing two tables instead of one (LaTex):

##
## \begin{table}[!htbp] \centering
## \caption{Logistic regression of organizational characteristics on the adoption of HR analytics}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}}lcc}
## \\[-1.8ex]\hline
## \hline \\[-1.8ex]
## \\[-1.8ex] & \multicolumn{2}{c}{Adoption of HR analytics} \\
## \\[-1.8ex] & (1) & (2)\\
## \hline \\[-1.8ex]
## Size (log) & 0.820$^{**}$ & 0.781$^{*}$ \\
## & (0.026, 1.614) & ($-$0.104, 1.666) \\
## Private sector & 0.077 & $-$0.283 \\
## & ($-$0.695, 0.850) & ($-$1.213, 0.648) \\
## MNE & 0.467 & 0.260 \\
## & ($-$0.237, 1.172) & ($-$0.516, 1.036) \\
## Org. age & $-$0.003 & $-$0.004 \\
## & ($-$0.009, 0.002) & ($-$0.010, 0.002) \\
## Strategic integration & & 0.176 \\
## & & ($-$0.313, 0.665) \\
## Devolvement of HRM & & $-$0.174 \\
## & & ($-$0.725, 0.376) \\
## Trade unions’ influence & & $-$0.138 \\
## & & ($-$0.444, 0.168) \\
## Intercept & $-$2.129$^{*}$ & $-$1.039 \\
## & ($-$4.456, 0.197) & ($-$4.247, 2.168) \\
## \textit{N} & 178 & 145 \\
## Log Likelihood & $-$119.004 & $-$95.994 \\
## Akaike Inf. Crit. & 248.008 & 207.989 \\
## \hline
## \hline \\[-1.8ex]
## \textit{Notes:} & \multicolumn{2}{l}{\parbox[t]{\textwidth}{Logistic regression. Dependent variable: ## \end{tabular}
## \end{table}
##
## \begin{table}[!htbp] \centering
## \caption{Logistic regression of organizational characteristics on the adoption of HR analytics}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}} c}
## \\[-1.8ex]\hline \\[-1.8ex]
## TRUE \\
## \hline
## \hline \\[-1.8ex]
## \multicolumn{1}{l}{\parbox[t]{\textwidth}{Logistic regression. Dependent variable: an indicator varible ## \end{tabular}
## \end{table}

Has anyone experienced similar issues and/or has an idea how to fix them? Thank you very much!

EDIT: Here I reproduced the error using the iris dataset

---
title: "Reproduction for StackOverflow using iris"
author: "M.Rapp"
date: "4 5 2022"
output: pdf_document
---    

```{r loading, include = FALSE, message=FALSE, warning=FALSE, error=FALSE}

library(foreign)
library(dplyr)
library(haven)
library(tidyverse)
library(tidymodels)
library(lme4)
library(ggplot2)
library(stargazer)
library(knitr)
library(foreign)
library(rmarkdown)
library(tinytex)


options(scipen = 999) # avoid scientific notation
options(max.print = 2000) # print up to 2000 lines


```

```{r tabelle, include = TRUE, echo = FALSE, results='asis', message = FALSE}

ir_data<- iris

set.seed(100)
samp<-sample(1:100,80)
ir_test<-ir_data[samp,]
ir_ctrl<-ir_data[-samp,]

y <- ir_test$Species
x <- ir_test$Sepal.Length
glfit<-glm(y~x, family = 'binomial')



stargazer(glfit, type = "latex",  title = "Logistic regression on species", no.space = TRUE, dep.var.labels="Species", covariate.labels = c("Sepal length", "Intercept"), ci = TRUE, odd.ratio = TRUE, header = FALSE)

```


Solution 1:[1]

You can use the package called gtsummary to make regression tables.

Replace your stargazer line with this:

library(gtsummary)
tbl_regression(glfit, exponentiate = TRUE,
               intercept = TRUE) %>% 
    add_significance_stars() %>% 
    add_glance_source_note(include = c(AIC, BIC, df.residual,nobs)) %>% 
    modify_caption("Logistic regression on species")

The tbl_regression() creates a basic table and the other functions called after pipes further customize the table.

see here for more details: https://www.danieldsjoberg.com/gtsummary/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike