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?

不羁的心 提交于 2020-02-29 21:40:12

问题


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?


回答1:


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




回答2:


As others note, the issue arises with special characters in the covariate.labels argument. The recommend 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")

  2. 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$")

  3. 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      
    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("`",   "'") 
    }


来源:https://stackoverflow.com/questions/54113706/how-to-fix-error-in-if-nchartext-matrixr-c-max-lengthreal-c-miss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!