Skip to content

Instantly share code, notes, and snippets.

@yihui
Last active June 13, 2021 17:45
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save yihui/6091942 to your computer and use it in GitHub Desktop.
Save yihui/6091942 to your computer and use it in GitHub Desktop.
use knitr (knit2pdf) to generate a PDF report in a Shiny app

To see this app in action, run this in R:

library(shiny)
runGist('https://gist.github.com/yihui/6091942')
\documentclass{article}
\begin{document}
<<names>>=
input$firstname
input$lastname
@
\end{document}
library(knitr)
shinyServer(function(input, output) {
output$report = downloadHandler(
filename = 'myreport.pdf',
content = function(file) {
out = knit2pdf('input.Rnw', clean = TRUE)
file.rename(out, file) # move pdf to file for downloading
},
contentType = 'application/pdf'
)
})
library(shiny)
shinyUI(basicPage(
textInput('firstname', 'First name', value = 'Jimmy'),
textInput('lastname', 'Last name', value = 'John'),
downloadButton('report')
))
@Kamakshaiah
Copy link

Kamakshaiah commented Jan 8, 2019

Hi yihui,
This is really a very helpful information. However, I am getting the following warning while executing "app".

Warning in file.rename(out, file) : cannot rename file 'input.pdf' to '/tmp/RtmpAeLXey/file1c6b135b80b9.pdf', reason 'Invalid cross-device link'

No output file is being created after...
Will you please help me how to solve this? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment