Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

r markdown - knitr cannot find pdflatex when creating pdf from shiny app

I am trying to create a shiny App that offers users a choice of canned reports from a database that renders reactively, and then offers users the option to download a pdf of their desired report. I am running into trouble getting knitr to discover pdflatex to make the pdf. I originally tried using MikTex, but on advice of other posts removed MikTex and instead used tinytex. I followed the guidance here: https://yihui.org/tinytex/r/#debugging. The shiny app works fine, but when I click the download button, R initially starts the process of compiling, but once I click save when the dialog button launches, R throws this error (I include the verbose output as suggested by Yuhui Xie - the tinytex package author):

processing file: Reports.Rmd
  |..................                                                      |  25%
  ordinary text without R code

  |....................................                                    |  50%
label: unnamed-chunk-1
  |......................................................                  |  75%
  ordinary text without R code

  |........................................................................| 100%
label: unnamed-chunk-2

output file: Reports.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS Reports.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc5ac42d2964ee.tex --lua-filter "C:UsersSCMCKENZIEDocumentsRwin-library4.0
markdown
markdownluapagebreak.lua" --lua-filter "C:UsersSCMCKENZIEDocumentsRwin-library4.0
markdown
markdownlualatex-div.lua" --self-contained --highlight-style tango --pdf-engine xelatex --variable graphics --variable "geometry:margin=1in" 
Warning: Error in : LaTeX failed to compile C:UsersSCMCKE~1AppDataLocalTempRtmp0a6jRkfile5ac45df413ce.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
  [No stack trace available]

Here is my SessionInfo()

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] plyr_1.8.6           rmarkdown_2.6        knitr_1.29          
 [4] tmap_3.3             rgeos_0.5-3          sp_1.4-1            
 [7] sf_0.9-7             xtable_1.8-4         readr_1.4.0         
[10] shinydashboard_0.7.1 odbc_1.2.3           DBI_1.1.0           
[13] shiny_1.5.0         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         lattice_0.20-41    png_0.1-7          class_7.3-17      
 [5] digest_0.6.25      mime_0.9           R6_2.4.1           evaluate_0.14     
 [9] e1071_1.7-3        pillar_1.4.6       rlang_0.4.7        rstudioapi_0.11   
[13] raster_3.3-13      blob_1.2.1         stringr_1.4.0      htmlwidgets_1.5.1 
[17] bit_1.1-15.2       tinytex_0.29       compiler_4.0.2     httpuv_1.5.4      
[21] xfun_0.20          pkgconfig_2.0.3    tmaptools_3.1-1    base64enc_0.1-3   
[25] htmltools_0.5.0    tidyselect_1.1.0   tibble_3.0.3       codetools_0.2-16  
[29] XML_3.99-0.4       viridisLite_0.3.0  crayon_1.3.4       dplyr_1.0.0       
[33] later_1.1.0.1      grid_4.0.2         jsonlite_1.7.0     lwgeom_0.2-5      
[37] lifecycle_0.2.0    magrittr_1.5       units_0.6-7        KernSmooth_2.23-17
[41] stringi_1.4.6      promises_1.1.1     leafsync_0.1.0     leaflet_2.0.4.1   
[45] ellipsis_0.3.1     generics_0.0.2     vctrs_0.3.2        RColorBrewer_1.1-2
[49] tools_4.0.2        dichromat_2.0-0    bit64_0.9-7        leafem_0.1.3      
[53] glue_1.4.1         purrr_0.3.4        hms_0.5.3          crosstalk_1.1.0.1 
[57] abind_1.4-5        parallel_4.0.2     fastmap_1.0.1      yaml_2.2.1        
[61] stars_0.5-1        classInt_0.4-3   

I have ensured that I have selected my Global Options in RStudio to use knitr and pdflatex. Sys.which('pdflatex') gives me:

         pdflatex 
"C:\Users\SCMCKE~1\AppData\Roaming\TinyTeX\bin\win32\pdflatex.exe"  

And tinytex::tlmgr_conf() gives me:

tlmgr conf
=========================== version information ==========================
tlmgr revision 57424 (2021-01-15 03:30:58 +0100)
tlmgr using installation: C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX
TeX Live (https://tug.org/texlive) version 2020
==================== executables found by searching PATH =================
PATH: C:UsersSCMCKE~1AppDataRoamingTinyTeXlpkglperlin;C:UsersSCMCKE~1AppDataRoamingTinyTeXinwin32;C:
tools40usrin;C:Program FilesRR-4.0.3inx64;C:Program FilesCommon FilesOracleJavajavapath;C:Program Files (x86)Common FilesOracleJavajavapath;C:Program FilesMicrosoft MPIBin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)Microsoft SQL Server150DTSBinn;C:Program Files (x86)Microsoft SQL Server150ToolsBinn;C:Program FilesMicrosoft SQL Server150ToolsBinn;C:Program FilesMicrosoft SQL Server150DTSBinn;C:Program FilesMicrosoft SQL ServerClient SDKODBC170ToolsBinn;C:Program FilesMicrosoft SQL Server130ToolsBinn;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WINDOWSSystem32OpenSSH;C:UsersSCMCKENZIEAppDataRoamingTinyTeXinwin32;C:UsersSCMCKENZIEAppDataLocalMicrosoftWindowsApps;C:UsersSCMCKENZIEAppDataLocalProgramsMiKTeXmiktexinx64
dvipdfmx:  C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/dvipdfmx.EXE
dvips:     C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/dvips.EXE
fmtutil:   C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/fmtutil.EXE
kpsewhich: C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/kpsewhich.EXE
luatex:    C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/luatex.EXE
mktexpk:   C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/mktexpk.EXE
pdftex:    C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/pdftex.EXE
tex:       C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/tex.EXE
tlmgr:     C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/tlmgr.BAT
updmap:    C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/updmap.EXE
xetex:     C:/Users/SCMCKE~1/AppData/Roaming/TinyTeX/bin/win32/xetex.EXE
=========================== active config files ==========================
config.ps:        c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist/dvips/config/config.ps
fmtutil.cnf:      c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist/web2c/fmtutil.cnf
mktex.cnf:        c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist/web2c/mktex.cnf
pdftexconfig.tex: c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/tex-ini-files/pdftexconfig.tex
texmf.cnf:        c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf.cnf
texmf.cnf:        c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist/web2c/texmf.cnf
updmap.cfg:       c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist/web2c/updmap.cfg
============================= font map files =============================
kanjix.map:  c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var/fonts/map/dvipdfmx/updmap/kanjix.map
pdftex.map:  c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var/fonts/map/pdftex/updmap/pdftex.map
ps2pk.map:   c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var/fonts/map/dvips/updmap/ps2pk.map
psfonts.map: c:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var/fonts/map/dvips/updmap/psfonts.map
=========================== kpathsea variables ===========================
ENCFONTS=.;{C:/PROGRA~1/R/R-40~1.2/share/texmf,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}/fonts/enc//
SYSTEXMF=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var;C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local;C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist
TEXCONFIG={C:/PROGRA~1/R/R-40~1.2/share/texmf,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}/dvips//
TEXFONTMAPS=.;{C:/PROGRA~1/R/R-40~1.2/share/texmf,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}/fonts/map/{kpsewhich,pdftex,dvips,}//
TEXMF={C:/PROGRA~1/R/R-40~1.2/share/texmf,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}
TEXMFCONFIG=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config
TEXMFDBS={!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}
TEXMFDIST=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist
TEXMFHOME=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local
TEXMFLOCAL=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local
TEXMFMAIN=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist
TEXMFSYSCONFIG=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config
TEXMFSYSVAR=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var
TEXMFVAR=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var
TEXPSHEADERS=.;{C:/PROGRA~1/R/R-40~1.2/share/texmf,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}/{dvips,fonts/{enc,type1,type42,type3}}//
VARTEXFONTS=C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var/fonts
WEB2C={C:/PROGRA~1/R/R-40~1.2/share/texmf,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-local,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-config,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-var,!!C:/Users/SCMCKENZIE/AppData/Roaming/TinyTeX/texmf-dist}/web2c
==== kpathsea variables from environment only (ok if no output here) ====

In c


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After much searching and testing, I found out that the problem was not with tinytex or pdflatex, but rather my call to rmarkdown::render() That led me to this answer on GitHub https://github.com/rstudio/shiny/issues/1831. Apparently, when rmarkdown::render() is called within downloadHandler(), you should NOT specify the output_file argument in render() Instead you need to rename the file AFTER rendering.

Here is the correct code (simply replace the call to downloadHandler() in the script from my original question.

  output$DWNLD<-downloadHandler(
    
    filename=paste0(input$REPORT, ".pdf"),
    
    content<-function(file){
      RPT<-list(iris, airquality)
      names(RPT)<-c("Iris", "AQ")
      
      tmp <- file.path(tempdir(), "Reports.Rmd", fsep="\")
      file.copy("F:/Apps/DNR_TRIS/Shiny_report_scripts/SQL_SCRIPTS/Reports.Rmd", tmp, overwrite = TRUE)
      out<-RPT[[input$REPORT]]
      params<-list(out)
      names(params)<-"DF"
      
      out<-rmarkdown::render(tmp,
                        params = params,
                        envir = new.env(parent = globalenv()
                        ))#End render
      file.rename(out, file)
    }
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...