yonicd/texPreview

texPreview not rendering {gt} + {gtsummary} latex output

annacnev opened this issue Β· 25 comments

Problem Description:

According to the documentation for texPreview, raw LaTeX code can be passed to the tex_preview function to render a preview. Currently, this works inconsistently.

Reproducible Code Snippet

df <- dplyr::tribble(  
  ~N,    ~Header1 ,   ~Header2,     ~Header3,
  11, 1,   3,     5,
  12, 2,   4,     6)

tex <- df %>% 
  tbl_summary(statistic = list(all_continuous() ~ "{mean} ({sd})"), label= list(vars(N) ~ 'N', vars(Header1) ~ 'something'), percent = c('row')) %>% as_gt() %>% as_latex() %>% as.character()

texPreview::tex_preview(obj = tex,stem = 'eq',imgFormat = 'svg')

Output

This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
 [1] "This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex 2019.12.9)  7 JAN 2020 10:55"
 [2] "entering extended mode"                                                                                                
 [3] " restricted \\write18 enabled."                                                                                        
 [4] " %&-line parsing enabled."                                                                                             
 [5] "**/private/var/folders/vz/bb360jr941d2crjjk2v3kjnw0000gp/T/Rtmpe8XId5/eqDoc.tex"                                       
 [6] ""                                                                                                                      
 [7] "(/private/var/folders/vz/bb360jr941d2crjjk2v3kjnw0000gp/T/Rtmpe8XId5/eqDoc.tex"                                        
 [8] "LaTeX2e <2019-10-01> patch level 1"                                                                                    
 [9] ""                                                                                                                      
[10] "! LaTeX Error: File `standalone.cls' not found."                                                                       
[11] ""                                                                                                                      
[12] "Type X to quit or <RETURN> to proceed,"                                                                                
[13] "or enter new name. (Default extension: cls)"                                                                           
[14] ""                                                                                                                      
[15] "Enter file name: "                                                                                                     
[16] "! Emergency stop."                                                                                                     
[17] "<read *> "                                                                                                             
[18] "         "                                                                                                             
[19] "l.2 \\usepackage"                                                                                                      
[20] "               [usenames,dvispnames,svgnames,table]{xcolor}^^M "                                                       
[21] "Here is how much of TeX's memory you used:"                                                                            
[22] " 12 strings out of 494111"                                                                                             
[23] " 404 string characters out of 6172989"                                                                                 
[24] " 54394 words of memory out of 5000000"                                                                                 
[25] " 4221 multiletter control sequences out of 15000+600000"                                                               
[26] " 3640 words of font info for 14 fonts, out of 8000000 for 9000"                                                        
[27] " 14 hyphenation exceptions out of 8191"                                                                                
[28] " 11i,0n,8p,155b,10s stack positions out of 5000i,500n,10000p,200000b,80000s"                                           
[29] ""                                                                                                                      
[30] "!  ==> Fatal error occurred, no output PDF file produced!"
Error in tex_image(obj, stem, write_flag, overwrite) : pdf not rendered

tex_preview is an S3 method that supports a number of classes built in.

what class is tbl_summary?

please revise the example to be reproducible

df <- dplyr::tribble(  
  ~N,    ~Header1 ,   ~Header2,     ~Header3,
  11, 1,   3,     5,
  12, 2,   4,     6)

tex <- df %>% 
  tbl_summary(statistic = list(all_continuous() ~ "{mean} ({sd})"), label= list(vars(N) ~ 'N', vars(Header1) ~ 'something'), percent = c('row')) %>% as_gt() %>% as_latex() %>% as.character()
#> Error in df %>% tbl_summary(statistic = list(all_continuous() ~ "{mean} ({sd})"), : could not find function "%>%"

Created on 2020-01-07 by the reprex package (v0.3.0)

Please provide the sessioninfo::session_info().

In addition please refer to the System requirements in the README.

System Requirements:
- Must have pdflatex in PATH, Windows users can install by running installr::install.MikTeX
- TeX libraries that are used: standalone, xcolor, booktabs, multirow, array, helvet, amsmath, rotating, listings, graphicx, setspace, caption

From the error you are getting it is apparent that you do not have standalone.cls installed on your system.

Once the system is aligned with the requirements please resubmit a reproducible output.

@yonicd what is the best way to install the new tex libraries on linux/mac?

@annacnev please help create a PR in the README with whatever the proper instructions are - i'm guessing something with tlmgr or the like?

the commands in travis.yml should suffice as instructions

@yonicd The class is character, the last pipe function is:

 %>% as.character()

Also, standalone is installed on my Mac. I am able to use texpreview with no issues on latex code when its rendered using Hmisc::latex like this:

tabtex <- capture.output(
  Hmisc::latex(
    tab,
    file = '',
    title='',
    caption='something',
    label=stem,
    rowname=NULL,
    colheads=c(names(tab %>% select(-CI)), "95\\% CI"),
    # col.just=c(rep("l", 2), rep("r",5)),
    # collabel.just = c(rep("l",2), "r", rep("l", 4)),
    # cgroup= c("Category", "$\\mathbf{AUC}_{0-24}$ (mcg $\\cdot$ h/L)", "Number of subjects"), 
    # n.cgroup = c(1, 2, 4), 
    where='!htb',
    landscape=FALSE,
    insert.bottom=paste0(
      '{\\small 
\\begin{flushleft}
     fdklafjdslafjsdfsafd). \\newline
 fjldsajdlsakfjdlsf. \\newline \\newline

     Source code: ', scriptname, '\\newline
     Source tex: ./deliv/table/', stem, '.tex
\\end{flushleft}}'
    )
  )
)

texPreview(
  tabtex,
  stem = 'test', 
  fileDir = tabDir , 
  cleanup = FALSE, 
  keep_pdf = TRUE
) 

So its not an issue with dependencies on my computer. It's an issue with how the LaTeX code is being written. In the LaTeX log shows the issue happens here:

"<read *> "                                                                                                             
[18] "         "                                                                                                             
[19] "l.2 \\usepackage"                                                                                                      
[20] "               [usenames,dvispnames,svgnames,table]{xcolor}^^M "  

Which means its stopping at this part of the rendered code because it's invalid.

@annacnev its reproducible in your session as you've loaded some other packages elsewhere.

To yoni's point, restart your r-session and you'll find %>% is not actually loaded. This likely comes from previously having loaded gt/dplyr/etc

This is where, given runnable code, reprex::reprex() is helpful as it runs the snippet in a new rsession forcing you to see if it is truly reproducible.

FWIW ... I'm getting issue with longtable; this was run locally, but get the same thing on Metworx

.libPaths("/data/Rlibs")

library(tidyverse)
library(gt)
library(gtsummary)
df <- dplyr::tribble(  
  ~N,    ~Header1 ,   ~Header2,     ~Header3,
  11, 1,   3,     5,
  12, 2,   4,     6)

tex <- df %>% 
  tbl_summary(
    statistic = list(all_continuous() ~ "{mean} ({sd})"), 
    label= list(vars(N) ~ 'N', vars(Header1) ~ 'something'), 
    percent = c('row')) %>% as_gt() %>% as_latex() %>% as.character()
#> Warning: The `cells_data()` function is deprecated and will soon be removed
#>  * Use the `cells_body()` function instead

texPreview::tex_preview(obj = tex,stem = 'eq',imgFormat = 'svg')
#> Error in tex_preview.default(obj, tex_lines, stem, overwrite, keep_pdf, :  
#> ! LaTeX Error: Environment longtable undefined.
#> 
#> See the LaTeX manual or LaTeX Companion for explanation.
#> Type H <return> for immediate help.
#>  ... 
#>  
#> l.2 \begin{longtable}
#>  {lc} 
#> !  ==> Fatal error occurred, no output PDF file produced!

cat(tex, sep = "\n")
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \begin{longtable}{lc}
#> \toprule
#> \textbf{Characteristic}\textsuperscript{1} & \textbf{N = 2} \\ 
#> \midrule
#> N &  \\ 
#> 11 & 1 (100\%) \\ 
#> 12 & 1 (100\%) \\ 
#> something &  \\ 
#> 1 & 1 (100\%) \\ 
#> 2 & 1 (100\%) \\ 
#> Header2 &  \\ 
#> 3 & 1 (100\%) \\ 
#> 4 & 1 (100\%) \\ 
#> Header3 &  \\ 
#> 5 & 1 (100\%) \\ 
#> 6 & 1 (100\%) \\ 
#> \bottomrule
#> \end{longtable}
#> \vspace{-5mm}
#> \begin{minipage}{\linewidth}
#> \textsuperscript{1}Statistics presented: n (\%) \\ 
#> \end{minipage}

Created on 2020-01-07 by the reprex package (v0.3.0)

thats my bad guys, I didn't copy the line where I loaded in packages. Sorry!

Here is the line:

library(gt)
library(gtsummary)

Regarding Hmisc:

As I have stated previously tex_preview is defined as an S3 method. Hmisc objects outputs are not built into texpreview and may contain side effects that are not compatible with the texpreview tex template.

I would suggest creating a tex_preview.hmisc function that would adjust the hmisc output so tex_preview can render it.

@kylebaron try running with adding the user package 'longtable' it is not in the template by default

tex_preview(...,usrPackages = texPreview::build_usepackage('longtable'))

@annacnev please paste in the sessioninfo::session_info() so i can install the same versions of packages as you have installed

gt and gt_summary are not on cran so i need to have that information to be able to resolve your issue.

> install.packages('gt')
Warning in install.packages :
  package β€˜gt’ is not available (for R version 3.6.1)
> install.packages('gt_summary')
Warning in install.packages :
  package β€˜gt_summary’ is not available (for R version 3.6.1)

thank you

I am using the latest version of both, here's the install instructions:

install.packages("remotes")
remotes::install_github("rstudio/gt")

and gtsummary:

install.packages("gtsummary")
library(gt)
library(gtsummary)
library(texPreview)

df <- dplyr::tribble(  
  ~N,    ~Header1 ,   ~Header2,     ~Header3,
  11, 1,   3,     5,
  12, 2,   4,     6)

tex <- df %>% 
  tbl_summary(statistic = list(all_continuous() ~ "{mean} ({sd})"), label= list(vars(N) ~ 'N', vars(Header1) ~ 'something'), percent = c('row')) %>% as_gt() %>% as_latex() %>% as.character()
#> Warning: The `cells_data()` function is deprecated and will soon be removed
#>  * Use the `cells_body()` function instead

print(tex_preview(tex,usrPackages = texPreview::build_usepackage('longtable'),returnType = 'tex'))
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \begin{longtable}{lc}
#> \toprule
#> \textbf{Characteristic}\textsuperscript{1} & \textbf{N = 2} \\ 
#> \midrule
#> N &  \\ 
#> 11 & 1 (100\%) \\ 
#> 12 & 1 (100\%) \\ 
#> something &  \\ 
#> 1 & 1 (100\%) \\ 
#> 2 & 1 (100\%) \\ 
#> Header2 &  \\ 
#> 3 & 1 (100\%) \\ 
#> 4 & 1 (100\%) \\ 
#> Header3 &  \\ 
#> 5 & 1 (100\%) \\ 
#> 6 & 1 (100\%) \\ 
#> \bottomrule
#> \end{longtable}
#> \vspace{-5mm}
#> \begin{minipage}{\linewidth}
#> \textsuperscript{1}Statistics presented: n (\%) \\ 
#> \end{minipage}

details::details(sessioninfo::session_info(),summary = 'session info')
session info
─ Session info ──────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.1 (2019-07-05)
 os       macOS Mojave 10.14.5        
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-01-07                  

─ Packages ──────────────────────────────────────────────────────────────
 package     * version    date       lib source                         
 askpass       1.1        2019-01-13 [1] CRAN (R 3.6.0)                 
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                 
 backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.0)                 
 base64enc     0.1-3      2015-07-28 [1] CRAN (R 3.6.0)                 
 checkmate     1.9.4      2019-07-04 [1] CRAN (R 3.6.0)                 
 cli           2.0.0      2019-12-09 [1] CRAN (R 3.6.0)                 
 clipr         0.7.0      2019-07-23 [1] CRAN (R 3.6.0)                 
 colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)                 
 commonmark    1.7        2018-12-01 [1] CRAN (R 3.6.0)                 
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                 
 desc          1.2.0      2019-12-01 [1] Github (r-lib/desc@61205f6)    
 details       0.1.4      2020-01-06 [1] local                          
 digest        0.6.23     2019-11-23 [1] CRAN (R 3.6.0)                 
 dplyr         0.8.3      2019-07-04 [1] CRAN (R 3.6.0)                 
 ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.0)                 
 evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)                 
 fansi         0.4.0      2018-10-05 [1] CRAN (R 3.6.0)                 
 fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)                 
 ggplot2       3.2.1      2019-08-10 [1] CRAN (R 3.6.0)                 
 glue          1.3.1.9000 2020-01-07 [1] Github (tidyverse/glue@b9ffe6c)
 gt          * 0.1.0      2020-01-07 [1] Github (rstudio/gt@a063c5f)    
 gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                 
 gtsummary   * 1.2.4      2019-12-17 [1] CRAN (R 3.6.0)                 
 highr         0.8        2019-03-20 [1] CRAN (R 3.6.0)                 
 htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.0)                 
 httr          1.4.1      2019-08-05 [1] CRAN (R 3.6.0)                 
 knitr         1.25       2019-09-18 [1] CRAN (R 3.6.0)                 
 lazyeval      0.2.2      2019-03-15 [1] CRAN (R 3.6.0)                 
 lifecycle     0.1.0      2019-08-01 [1] CRAN (R 3.6.0)                 
 magick        2.2        2019-08-26 [1] CRAN (R 3.6.0)                 
 magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                 
 munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)                 
 pdftools      2.2        2019-03-10 [1] CRAN (R 3.6.0)                 
 pillar        1.4.3      2019-12-20 [1] CRAN (R 3.6.0)                 
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 3.6.0)                 
 png           0.1-7      2013-12-03 [1] CRAN (R 3.6.0)                 
 purrr         0.3.3      2019-10-18 [1] CRAN (R 3.6.0)                 
 qpdf          1.1        2019-03-07 [1] CRAN (R 3.6.0)                 
 R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.0)                 
 Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.1)                 
 rematch2      2.1.0      2019-07-11 [1] CRAN (R 3.6.0)                 
 rlang         0.4.2      2019-11-23 [1] CRAN (R 3.6.0)                 
 rmarkdown     2.0        2019-12-12 [1] CRAN (R 3.6.0)                 
 rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)                 
 rstudioapi    0.10       2019-03-19 [1] CRAN (R 3.6.0)                 
 scales        1.1.0      2019-11-18 [1] CRAN (R 3.6.0)                 
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                 
 stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)                 
 stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                 
 svgPanZoom    0.3.3      2016-09-26 [1] CRAN (R 3.6.0)                 
 texPreview  * 1.4.3      2020-01-07 [1] local                          
 tibble        2.1.3      2019-06-06 [1] CRAN (R 3.6.0)                 
 tidyr         1.0.0      2019-09-11 [1] CRAN (R 3.6.0)                 
 tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.6.0)                 
 vctrs         0.2.1      2019-12-17 [1] CRAN (R 3.6.0)                 
 whisker       0.4        2019-08-28 [1] CRAN (R 3.6.1)                 
 withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                 
 xfun          0.10       2019-10-01 [1] CRAN (R 3.6.0)                 
 xml2          1.2.2      2019-08-09 [1] CRAN (R 3.6.0)                 
 yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)                 
 zeallot       0.1.0      2018-01-28 [1] CRAN (R 3.6.0)                 

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

Created on 2020-01-07 by the reprex package (v0.3.0)

image

image


It appears that your system is still not conforming with the requirements. it is missing the helvet library.

System Requirements:
- Must have pdflatex in PATH, Windows users can install by running installr::install.MikTeX
- TeX libraries that are used: standalone, xcolor, booktabs, multirow, array, helvet, amsmath, rotating, listings, graphicx, setspace, caption

I noticed that and tried to do:

tlmgr install helvet 

but was getting error

tlmgr install: package helvet not present in repository.

but doing :

tlmgr install psnfss

was the fix. Posting incase someone runs into something similar.

Possible idea to resolve all future package dependency issues is to add a tinytex dependency and do:

tinytex::tlmgr_install(pkgs = c('psnfss', 'standalone', 'listings'.... (all required tex packages) ))

which will check and install any missing required packages before the preview is rendered?

closing this issue.

tinytex dependency should be a different thread. it was checked in the past and decided against it.

There is code in the package .onLoad checking for packages that are required for rendering the template.

https://github.com/metrumresearchgroup/texPreview/blob/791e0c9b1cef888d1e981f7bd140546c04a35f29/R/zzz.R#L9_L19

I'm going to re-open this as I think there are some discussions/learnings still to be had:

  1. which will check and install any missing required packages before the preview is rendered?

@annacnev this is a nice thought, however there are some implications of this that we like to not encourage. Primarily, doing things on the users behalf to try to be 'helpful' is a slippery slope. Better to instead error gracefully and give them the option to take matters into their own hands to resolve the issue. This also allows users that don't have suitable privileges, have non-standard configurations, etc from accidentally getting their system into an even more inconsistent state.

  1. After trying myself, I think this comes down to tinytex being too stripped down of a distribution. I had to incrementally burrow my way through dependencies, including chasing psnfss as the true dependency for helvet. I ended up getting stuck still with some perl dep.

Instead, I would just install mactex - its a much heavier distribution

brew cask install mactex

but comes with everything you will need and is much closer to the metworx configuration we have anyway, and therefore we won't have to burn more cycles on dependency reconciliation.

  1. With respect to reproducing the example, I encourage you to revisit: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

In particular, as you discovered, making sure the example can run end-to-end is important, and can be checked via restarting and clearing your r-session or using a reprex. Furthermore, it is always good practice to include your session information via sessioninfo::session_info() as that will provide exact package versions etc so that if someone else is getting different results, we can also confirm environments are consistent.

Rather than taking on dependency, maybe export some of the check functionality? Might have been helpful in this case. Along with @annacnev suggestion, could you export a function like tex_pkgs() except have it return names that could get passed to tinytex::tlmgr_install? You could just point to the tinytex function in the help file and show an example rather than doing it for the user.

library(texPreview)

texPreview:::tex_pkgs()
#>  [1] "xcolor"   "booktabs" "multirow" "array"    "helvet"   "amsmath" 
#>  [7] "rotating" "listings" "graphicx" "setspace" "caption"

texPreview:::check_requirments()
#>   xcolor booktabs multirow    array   helvet  amsmath rotating listings 
#>     TRUE     TRUE     TRUE     TRUE     TRUE     TRUE     TRUE     TRUE 
#> graphicx setspace  caption 
#>     TRUE     TRUE     TRUE

Created on 2020-01-07 by the reprex package (v0.3.0)

@kylebaron exporting that function is a good idea. I do not understand yet why the .onLoad warning did not surface.

This should of happened .onLoad, which would've given the right guidance on how to diagnose the problem.

Warning messages:
1: In check_requirments() :
  missing tex packages needed for texPreview: xcolor, multirow, helvet, listings, setspace

@dpastoor This is also why I closed this issue with the comment to open this more specific issue/feature as a new one, since it will get lost inside this {gt} specific issue.

I would still rather migrate this new conversation to a new issue, that can be tracked and resolved in an organized fashion.

I am not sure if this should be categorized as gt specific because the gt object was converted to character and then feed into the texpreview function. Also, the solve ended up being missing dependencies + texPreview::build_usepackage('longtable'). After going through a lot of table examples in the reports, a lot of scientists use the longtable latex package without using gt so they may want this information too. I think it would be better to categorize this under longtable so its easily findable for future use.

Also, with the .onLoad, is it possible that is set to execute when the full package is loaded into the R session vs. just a single function from the package?

I think to @yonicd's point the scope of the solution is not related to gt in particular (so you're saying the same thing), and his (reasonable) request is that at this point we need to open up a more focused issue to have a discussion around both dependencies and installation instructions.

Also, with the .onLoad, is it possible that is set to execute when the full package is loaded into the R session vs. just a single function from the package?

That is how onLoad works - it executes when the library is first loaded. If you want function specific checks, you'll do that in the function (eg some functions will use suggested packages, so you need to check if those packages are installed before trying to execute downstream code.

Functions that check system requirements have been exported and a note has been added to the readme regarding them and tinytex installation.

4de3c47

PR #36

  • exported requirements functions
  • updated readme documentation to clarify system requirements

reclosing issue.