I use a combination of Shiny and knitr to create PDF documents.
Currently, I want to add a function that will allow the user to upload an image that will be placed in the created document. However, I am really stuck because I cannot get the path to the input picture. Can someone help me with this?
A simple example:
Application:
library(knitr) library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel( fileInput("picture", label = 'Picture'), downloadButton('report', label = 'Download PDF') ), mainPanel() ) ) server <- function(input,output){ picture <- reactive({ input$picture[,4] }) output$report = downloadHandler( filename = "test.pdf", content = function(file){ picture = picture() out = knit2pdf(input = 'test.Rnw', compiler = 'xelatex', clean = TRUE) file.rename(out, file) }, contentType = 'application/pdf' ) } shinyApp(ui = ui, server = server)
and .Rnw :
\documentclass{article} \begin{document} Put picture here: <<echo = FALSE , message = F, results='asis'>>= cat(paste('\\includegraphics[height=3in]{', picture,'}')) @ \end{document}
The '\includegraphics[height=3in]{', picture,'} obviously causes a problem because I don't know that the image path is only temporary.
r shiny knitr
An economist
source share