Search code examples
rshinyknitrpdflatexxelatex

Shiny + knitr does not load latex packages when compiling the documents


I have more open question about general problem with knitr that I recently discovered. When I compile documents via shiny simply with:

output$report = downloadHandler(
    filename = reactive({paste0(input$filename,'.pdf')}),

    content = function(file) {
      out = knit2pdf(input = 'pdf_shell.Rnw')
      file.rename(out, file)
    },

    contentType = 'application/pdf'
  )

Some latex packages as eso-pic or hyperref are not working and result in error Running ' texi2div ' on ' pdf_shell.tex ' failed. Including compiler = 'xelatex' into the knit2pdf helps with some functionality but corrupts other (For instance, \TextField function does not work and covers text).

Therefore, my question is does anyone know how I could compile the PDF using default compiler pdflatex, without getting the above mentioned error? Or you maybe have any pro tips that could solve the problems differently. Any input is much appreciated.

edit: I have to mention that when I run the app through R Studio everything works fine. App functionality ( and latex packages) breaks when I upload it to the shinyapps.io

edit 2: I discovered that when I include in my .Rnw file additional backslash the file compiles correctly. So version that is not working:

\begin{Form}
\begin{tabularx}{\textwidth}{p{8cm}}
Description \\
\TextField[name=1, multiline=true, width=\linewidth,height=0.6in, bordercolor = 1 1 1, charsize=0pt]{} \\
\end{tabularx}
\end{Form}

Version that works:

\begin{Form}
    \begin{tabularx}{\textwidth}{p{8cm}}
    Description \\\
    \TextField[name=1, multiline=true, width=\linewidth,height=0.6in, bordercolor = 1 1 1, charsize=0pt]{} \\\
    \end{tabularx}
    \end{Form}

Is someone able to explain my why it is the case?


Solution

  • After some research I found that some packages like hyperref (TextFields) generally might not be working with normal tables, when compiled with xelatex (proof), but should be compiled within p column, like this:

    \begin{Form}
    \begin{tabularx}{\textwidth}{p{8cm}}
    Description \\
    \TextField[name=1, multiline=true, width=\linewidth,height=0.6in, bordercolor = 1 1 1, charsize=0pt]{} \\
    \end{tabularx}
    \end{Form}
    

    However, it does not solve the problem within Shiny. To make it work I had to add one more backslash when splitting between rows (no clue why). So if you want to make it work you have to add additional backslash and the resulting code should look like this:

    \begin{Form}
        \begin{tabularx}{\textwidth}{p{8cm}}
        Description \\\
        \TextField[name=1, multiline=true, width=\linewidth,height=0.6in, bordercolor = 1 1 1, charsize=0pt]{} \\\
        \end{tabularx}
        \end{Form}