Search code examples
htmlrstargazer

R Stargazer Error - how to export as .html when add.lines argument is used?


How to output the stargazer results table as .html when using the add.lines argument in R?

My current approach is:

fem <- plm(model, data =pdata, effect = "twoways",model="within")

rob_se      <- sqrt(diag(vcovHC(fem, type = "HC1", cluster = "group")))                    
stargazer(fem,
          align=TRUE,
          type = "html",
          se = list(rob_se),
          out = "Test.html")

which works perfectly. Nevertheless, when using the add.line argument of the function such as

r2 <- 0.4
stargazer(fem, align=TRUE,
                          type = "html",
                          se = list(rob_se),
                          add.lines = list(c("$R^2$", r2)),
                          out = "Test.html")

I receive the following error message

Error in if (nchar(text.matrix[r, c]) > max.length[real.c]) { : 
  missing value where TRUE/FALSE needed

Here, the type = .html argument presents the issue, because I'm able to run the function without it. Unfortunately, the result is then formatted as a character in R.


Solution

  • Using the code with $R^2$ gives the error:

    stargazer(fit1_robust, fit2_robust, type = "html", 
              add.lines = list(c("$R^2$", fit1_r2, fit2_r2)),
              out = "path/nameofthetable.html")
    
    Error in if (nchar(text.matrix[r, c]) > max.length[real.c]) { : 
      missing value where TRUE/FALSE needed
    

    Changing it to R2 solves the problem:

    stargazer(fit1_robust, fit2_robust, type = "html", 
              add.lines = list(c("R2", fit1_r2, fit2_r2)),
              out = "path/nameofthetable.html")