hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
322 stars 28 forks source link

huxreg does not knit to pdf when the regression contains a squared term with the syntax I(var^2) #196

Closed nreigl closed 3 years ago

nreigl commented 3 years ago

Hi,

knitting a .Rmd to pdf fails for me when I include a squared term in the regression that should later be evaluated by huxreg() and printed in the pdf.

Steps to reproduce the problem

First, estimate a linear regression without a squared term, supply the model object to the huxreg function and knit the document to pdf works.

file1.Rmd

---
title: "huxtable_reprex"
author: "Nicolas Reigl"
date: "4/14/2021"
output: pdf_document
---

```{r}
library(huxtable)
data(mtcars)
lm_cars <- lm(mpg ~ hp, data = mtcars)
huxreg(lm_cars)

rmarkdown::render("file1.Rmd")


                             ─────────────────────────────────────────────────
                                                                         (1)  
                                                     ─────────────────────────
                               (Intercept)                        30.099 ***  
                                                                  (1.634)     
                               hp                                 -0.068 ***  
                                                                  (0.010)     
                                                     ─────────────────────────
                               N                                  32          
                               R2                                  0.602      
                               logLik                            -87.619      
                               AIC                               181.239      
                             ─────────────────────────────────────────────────
                               *** p < 0.001; ** p < 0.01; * p < 0.05.        

Column names: names, model1
processing file: file1.Rmd
  |........                                                              |  11%
  ordinary text without R code

  |................                                                      |  22%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |.......................                                               |  33%
  ordinary text without R code

  |...............................                                       |  44%
label: unnamed-chunk-1
  |.......................................                               |  56%
  ordinary text without R code

  |...............................................                       |  67%
label: unnamed-chunk-2
  |......................................................                |  78%
  ordinary text without R code

  |..............................................................        |  89%
label: unnamed-chunk-3
  |......................................................................| 100%
  ordinary text without R code

/usr/local/bin/pandoc +RTS -K512m -RTS huxtable.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output huxtable.tex --lua-filter /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable 'geometry:margin=1in' --include-in-header /var/folders/v4/cznjt18s39747098qv69_0s80000gn/T//Rtmp1QrwD4/rmarkdown-str7ce06d02caed.html 
output file: file1.knit.md

Output created: file1.pdf

Describe the problem in detail

I add a squared term to the regression in the standard R regression formula syntax I(hp^2).

Expected behaviour

Knitting the .Rmd produces a pdf document without failing in the compilation process.

Observed behaviour

Including a squared term in the regression, knitting to pdf fails, presumingly because the term I(hp^2) is not evaluated as mathematical expression that should be escaped in the latex compilation process.

huxtable.Rmd

---
title: "huxtable_reprex"
author: "Nicolas Reigl"
date: "4/14/2021"
output: pdf_document
---

```{r}
library(huxtable)
data(mtcars)
lm_cars_sq <- lm(mpg ~ hp + I(hp^2) , data = mtcars)
huxreg(lm_cars_sq)

rmarkdown::render("huxtable.Rmd") fails with


                         ─────────────────────────────────────────────────
                                                                     (1)  
                                                 ─────────────────────────
                           (Intercept)                        40.409 ***  
                                                              (2.741)     
                           hp                                 -0.213 ***  
                                                              (0.035)     
                           I(hp^2)                             0.000 ***  
                                                              (0.000)     
                                                 ─────────────────────────
                           N                                  32          
                           R2                                  0.756      
                           logLik                            -79.801      
                           AIC                               167.602      
                         ─────────────────────────────────────────────────
                           *** p < 0.001; ** p < 0.01; * p < 0.05.        

Column names: names, model1
> rmarkdown::render("huxtable.Rmd")

processing file: huxtable.Rmd
  |.......                                                                                          |   8%
  ordinary text without R code

  |...............                                                                                  |  15%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |......................                                                                           |  23%
  ordinary text without R code

  |..............................                                                                   |  31%
label: unnamed-chunk-7
  |.....................................                                                            |  38%
  ordinary text without R code

  |.............................................                                                    |  46%
label: unnamed-chunk-8
  |....................................................                                             |  54%
  ordinary text without R code

  |............................................................                                     |  62%
label: unnamed-chunk-9
  |...................................................................                              |  69%
  ordinary text without R code

  |...........................................................................                      |  77%
label: unnamed-chunk-10
  |..................................................................................               |  85%
  ordinary text without R code

  |..........................................................................................       |  92%
label: unnamed-chunk-11
  |.................................................................................................| 100%
  ordinary text without R code

output file: huxtable.knit.md

/usr/local/bin/pandoc +RTS -K512m -RTS huxtable.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output huxtable.tex --lua-filter /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable 'geometry:margin=1in' --include-in-header /var/folders/v4/cznjt18s39747098qv69_0s80000gn/T//RtmpCnh9hY/rmarkdown-str4ccd446b80eb.html 
! Missing $ inserted.
<inserted text> 
                $
l.301 ...I(hp\verb|^|2) \hspace{6pt}\huxbpad{6pt}}
                                                   & 

Try to find the following text in huxtable.Rmd:
  ...I(hp\verb|^|2) \hspace{6pt}\huxbpad{6pt}} 

You may need to add $ $ around a certain inline R expression `r ` in huxtable.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile huxtable.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See huxtable.log for more info.

System Info

sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] huxtable_5.2.0

loaded via a namespace (and not attached):
 [1] knitr_1.31        magrittr_2.0.1    tidyselect_1.1.0  R6_2.5.0          rlang_0.4.10     
 [6] fansi_0.4.2       stringr_1.4.0     dplyr_1.0.5       tools_4.0.4       broom_0.7.6      
[11] xfun_0.22         utf8_1.2.1        DBI_1.1.1         htmltools_0.5.1.1 ellipsis_0.3.1   
[16] yaml_2.2.1        assertthat_0.2.1  digest_0.6.27     tibble_3.1.0      lifecycle_1.0.0  
[21] crayon_1.4.1      tidyr_1.1.3       purrr_0.3.4       vctrs_0.3.7       glue_1.4.2       
[26] evaluate_0.14     rmarkdown_2.7     stringi_1.5.3     compiler_4.0.4    pillar_1.5.1     
[31] backports_1.2.1   generics_0.1.0    pkgconfig_2.0.3  

I have tried to reproduce this behaviour with the comparable stargazer package function and the modelsummary package function and in both cases I can knit the file huxtable.Rmd to a pdf and the regression table is correctly displayed.

hughjonesd commented 3 years ago

Thank you. Please try the latest github master and see if it fixes your problem.