How to fix 'Error in if (nchar(text.matrix[r, c]) > max.length[real.c]) { : missing value where TRUE/FALSE needed' using stargazer in rstudio?
Asked Answered
M

3

17

I'm trying to print a table of combined lm's in Rstudio using Stargazer and I keep getting this message:

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

This is the code I'm using:

stargazer(lm_1, lm_2, lm_3, lm_4, 
          dep.var.labels =  c("PolOri_Social_std", "Sexual_Disgust_std"), 
          covariate.labels = c("Gender", "Sexual_Disgust_std"), 
          style = "demography", 
          out = "hierarchical.htm",
          header = F)

Has anyone encountered this before?

Milliard answered 9/1, 2019 at 15:47 Comment(1)
I get the same error. It seems to a bug related to the "covariate.labels" argument. If you delete the covariate.labels argument, the table will print (obviously without renamed covariate labels).Wight
L
16

Problem seems to be related to using underscores in the names for covariates. Take these out of them and it should work.

Lashondra answered 13/2, 2019 at 13:25 Comment(2)
It can also be caused by the hat ^Hoppe
The same issue arises when there is a line break in the regression equation (that will record as \n).Feathercut
C
8

As others note, the issue arises with special characters in the covariate.labels argument. The recommended solutions, however, miss a few things:

  1. With latex output, you can use \\ to 'escape' special characters so that they appear properly. You can also use the command xtable::sanitize("Sexual_Disgust_std", type = "latex") to convert a string to something more latex friendly. In the original example, that would be:
`covariate.labels = c("Gender", "Sexual\\_Disgust\\_std")`
  1. With latex output, some special characters are mathematical and they need to be enclosed in a math mode syntax. For example, if two covariates were % Black and (% Black)-squared, one might write:
`covariate.labels = c("\\% Black", "(\\% Black)$^2$")`
  1. The original question appears to write to a .htm file but does not specify in stargazer type = 'html' so the default will be type = 'latex'. If switching between latex and html output, some of the latex encodings can break the html generation. I'm not aware of an elegant solution to this issue but if you're using knitr with R Markdown or Sweave, you could use the functions: knitr:: is_latex_output() or knitr::is_html_output() to generate latex or html appropriate code as in:
    library(knitr)
    library(dplyr)
    library(stargazer)
        
    star_format <- dplyr::case_when(
        knitr::is_latex_output() ~ "latex",
        knitr::is_html_output()  ~ "html",
        TRUE                     ~ "text" # for interactive coding in console
    )
    
    # One way would be to build latex / html specific labels
    covar_labels <- dplyr::case_when(
        knitr::is_latex_output() ~ c("Gender", "Sexual\\_Disgust\\_std"),
        knitr::is_html_output()  ~ c("Gender", "Sexual Disgust std"),
        TRUE                     ~ c("Gender", "Sexual Disgust std")
    )

    # for simplicity, stargazer call doesn't include custom dep.var.labels or out arguments      
    stargazer(lm_1, lm_2, lm_3, lm_4, 
          type = star_format,
          covariate.labels = covar_labels)
    
    
    # A second way would be to create separate stargazer calls:
    if(knitr::is_html_output()) {
    stargazer(lm_1, lm_2, lm_3, lm_4, 
          type             = star_format,
          dep.var.labels   = c("PolOri Social std", "Sexual Disgust std"), 
          covariate.labels = c("Gender", "Sexual Disgust std"), 
          style            = "demography", 
          out              = "hierarchical.html",
          header           = FALSE)
    }
    
    if(knitr::is_latex_output()) {
    stargazer(lm_1, lm_2, lm_3, lm_4, 
          type             = star_format,
          dep.var.labels   = c("PolOri\\_Social\\_std", "Sexual\\_Disgust\\_std"), 
          covariate.labels = c("Gender", "Sexual\\_Disgust\\_std"), 
          style            = "demography", 
          out              = "hierarchical.tex",
          header           = FALSE)
    }
  1. Using the same knitr::is_latex_output() and knitr::is_html_output() functions, one could also pre-process any labels with regex to be specifically formatted for html or latex output. For example, below is a small function that would search and replace various special characters text strings.
    library(stringr)

    remove_special_chars <- function(covar_labels){
        covar_labels %>% 
            str_replace_all("\\\\", "") %>% 
            str_replace_all("\\^",  "") %>%
            str_replace_all("_",   " ") %>% 
            str_replace_all("\\$",  "") %>% 
            str_replace_all("`",   "'") 
    }
Civil answered 27/12, 2019 at 19:31 Comment(0)
M
1

The error is caused because underscores (_) need to be escaped in Latex and that confuses stargazer (even if you're printing text or html).

For me, the easiest solution is to manually take out troublesome characters from labels and pass them to dep.var.labels or covariate.labels.

stargazer(lm_1, lm_2, lm_3, lm_4, 
          dep.var.labels = c("PolOri Social std", "Sexual Disgust std"), # Took out _'s
          covariate.labels = c("Gender", "Sexual Disgust std"))          # Took out _'s
Misery answered 3/5, 2022 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.