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')
))
@csliangy
Copy link

Hi, Yihui

I have posted the same message on your blog but I think this might be a better place to discuss the issue.

I published the exact the same sample app on shinyapps.io but I came cross the following issues (I can run locally on my windows as well as RStudio Server (On an Ubuntu)):

2018-10-26T01:48:44.985563+00:00 shinyapps[535924]: Server version: 1.6.8-6
2018-10-26T01:48:44.985565+00:00 shinyapps[535924]: LANG: en_US.UTF-8
2018-10-26T01:48:44.985618+00:00 shinyapps[535924]: rmarkdown version: NA
2018-10-26T01:48:44.985614+00:00 shinyapps[535924]: R version: 3.5.1
2018-10-26T01:48:44.985616+00:00 shinyapps[535924]: shiny version: 1.1.0
2018-10-26T01:48:45.133726+00:00 shinyapps[535924]:
2018-10-26T01:48:44.985619+00:00 shinyapps[535924]: knitr version: 1.20
2018-10-26T01:48:44.985617+00:00 shinyapps[535924]: httpuv version: 1.4.5
2018-10-26T01:48:45.128881+00:00 shinyapps[535924]: Using jsonlite for JSON processing
2018-10-26T01:48:45.172152+00:00 shinyapps[535924]: Listening on http://127.0.0.1:41445
2018-10-26T01:48:44.985620+00:00 shinyapps[535924]: RJSONIO version: NA
2018-10-26T01:48:44.985621+00:00 shinyapps[535924]: htmltools version: 0.3.6
2018-10-26T01:48:45.133728+00:00 shinyapps[535924]: Starting R with process ID: '18'
2018-10-26T01:48:44.985699+00:00 shinyapps[535924]: Using pandoc at /opt/connect/ext/pandoc2
2018-10-26T01:48:45.172150+00:00 shinyapps[535924]:
2018-10-26T01:48:44.985619+00:00 shinyapps[535924]: jsonlite version: 1.5
2018-10-26T01:48:54.630740+00:00 shinyapps[535924]:
2018-10-26T01:48:54.630742+00:00 shinyapps[535924]:
2018-10-26T01:48:54.630744+00:00 shinyapps[535924]: processing file: input.Rnw
2018-10-26T01:48:54.652924+00:00 shinyapps[535924]: ordinary text without R code
2018-10-26T01:48:54.652725+00:00 shinyapps[535924]:
|
| | 0%
|
|...................... | 33%
2018-10-26T01:48:54.652925+00:00 shinyapps[535924]:
2018-10-26T01:48:54.653358+00:00 shinyapps[535924]:
|
|........................................... | 67%
2018-10-26T01:48:54.654942+00:00 shinyapps[535924]: label: names
2018-10-26T01:48:54.729765+00:00 shinyapps[535924]: ordinary text without R code
2018-10-26T01:48:54.729719+00:00 shinyapps[535924]:
|
|.................................................................| 100%
2018-10-26T01:48:54.729766+00:00 shinyapps[535924]:
2018-10-26T01:48:54.730620+00:00 shinyapps[535924]:
2018-10-26T01:48:54.732189+00:00 shinyapps[535924]: output file: input.tex
2018-10-26T01:48:54.732189+00:00 shinyapps[535924]:
2018-10-26T01:48:54.734834+00:00 shinyapps[535924]: Warning: Error in loadNamespace: there is no package called ‘tinytex’
2018-10-26T01:48:54.739940+00:00 shinyapps[535924]: [No stack trace available]

Thanks,
Liang

@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