2016-03-04 134 views
3

我創建了一個非常大的閃亮應用程序,它具有可下載的pdf報告。客戶已經在pdf的每一頁的標題中請求了他們的標識。當pdf本身(不是更大的閃亮應用程序的一部分)時,我可以在PDF上獲得徽標,但是當我嘗試從閃亮的應用程序中下載完全相同的報告時,pandoc無法找到圖像。下面是一個最低工作示例,以及我嘗試過但未能開始工作的事情列表。 smiley.png位於app.R文件夾中,可以被任何圖像替換。 smiley.png與我在完整應用中使用的圖像不同,因此它與原始圖像無關。從Shiny應用程序下載的r markdown報告中使用圖像

針織rmarkdown本身完美,幷包括標題。嘗試從閃亮的應用程序下載會導致問題。
我已經試過:

  • 的圖像移動到一個名爲「圖像」,並引用圖片文件夾\ smiley.png而非smiley.png。同樣的錯誤也會失敗。
  • 當應用上傳到shinyapps.io時,引用/srv/shiny-server/AppName/smiley.png。無法找到圖像(相同類型的錯誤)失敗。
  • 使用普通的![Logo](smiley.png)語法而不是四個標題行。同樣的錯誤無法找到smiley.png
  • <img src="smiley.png" />與我的www文件夾中的smiley.png不起作用。我正在編寫PDF,而不是HTML。 pdf編織,但不包含圖像。它只是刪除html。
  • 使用plain ![Logo](smiley.png)與我的www文件夾中的smiley.png語法不起作用。同樣的錯誤;找不到smiley.png。

我最好的猜測是,當應用程序運行時,它以某種方式繞着目錄移動,並且.rmd無法找到圖像。那麼,爲了找到圖像我需要參考什麼?我可以把它放在一個特定的文件夾中嗎?我嘗試了很多不同的東西,並做了大量的研究,但找到一個類似的例子卻遇到了困難。我用閃光的應用程序中的圖片(不包含在下面)使用了www文件夾,添加了新文件夾,將圖片放在與.rmd相同的文件夾中......這是一個非常漫長的研究,試用,並沒有成功的錯誤。

的應用:

library(shiny) 
ui<-shinyUI(fluidPage(
    titlePanel("Hello Shiny!"), 
    sidebarPanel(
    downloadButton('downloadReport',label="Download Report") 
      ), 
    mainPanel(
     p("Hello") 
      ) 
)) 

server<-shinyServer(function(input, output) { 
    output$downloadReport <- downloadHandler(
     filename = function() { 
      paste0('Report_.pdf') 
          }, 
     content = function(file) { 
      src <- normalizePath('report.rmd') 
      owd <- setwd(tempdir()) 
      on.exit(setwd(owd)) 
      file.copy(src, 'report.rmd') 
      library(rmarkdown) 
      out <- render('report.rmd',pdf_document()) 
      file.rename(out, file) 
           } 
    ) 
}) 

shinyApp(ui, server)#Runs the app 

[R降價report.rmd:

--- 
title: "Test" 
date: "Friday, March 04, 2016" 
output: pdf_document 
header-includes: \usepackage{fancyhdr} 
--- 

\addtolength{\headheight}{1.0cm} 
\pagestyle{fancyplain} 
\lhead{\includegraphics[height=1.2cm]{smiley.png}} 
\renewcommand{\headrulewidth}{0pt} 

```{r, echo=FALSE} 
plot(cars) 
``` 
```{r, echo=FALSE} 
plot(cars) 
``` 

錯誤:

C:/Apps/RStudio/bin/pandoc/pandoc report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output report.pdf --template C:\Apps\R-3.1.1\library\rmarkdown\rmd\latex\default.tex --highlight-style tango --latex-engine pdflatex --variable geometry:margin=1in 
pandoc.exe: Error producing PDF from TeX source. 
! Package pdftex.def Error: File `smiley.png' not found. 

See the pdftex.def package documentation for explanation. 
Type H <return> for immediate help. 
...            

l.88 \end{document} 

Warning: running command 'C:/Apps/RStudio/bin/pandoc/pandoc report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output report.pdf --template C:\Apps\R-3.1.1\library\rmarkdown\rmd\latex\default.tex --highlight-style tango --latex-engine pdflatex --variable geometry:margin=1in' had status 43 
Error : pandoc document conversion failed with error 43 
In addition: Warning message: 
package ‘shiny’ was built under R version 3.1.3 
Warning: Error in : pandoc document conversion failed with error 43 
Stack trace (innermost first): 
    55: pandoc_convert 
    54: render 
    53: download$func [C:/Data/Documents/Technomic/Testing images/app.R#25] 
    5: <Anonymous> 
    4: do.call 
    3: print.shiny.appobj 
    2: print 
    1: source 

謝謝!我希望有人有一些想法。我一直在研究和嘗試幾天的事情。

編輯:修正了輸出格式。

+0

請格式化輸出爲代碼,作爲引號,所以它更容易閱讀您的問題。謝謝。 – Farside

+0

看看[這個答案](http://stackoverflow.com/questions/19434991/adding-local-image-with-html-to-a-shiny-app)。您需要創建一個'www'目錄並將其中的圖像。另請參閱本教程的圖片部分(http://shiny.rstudio.com/tutorial/lesson2/)以獲取更詳細的解釋。 – NicE

+0

對不起,我應該提到我正在渲染一個PDF,而不是HTML。 HTML不起作用。試圖插入並沒有出現任何內容。 pdf編織,但沒有圖像。此外,該圖片位於大型應用程序的我的www文件夾中,smiley.png仍無法找到它。我會將這添加到我嘗試的解決方案中。 – sfeliz92

回答

4

最後發現問題 - 它根本不在rmarkdown中。我正在將report.pdf複製到一個臨時目錄,以確保我可以寫入它,這意味着報告無法再找到該圖像。我所要做的就是將圖像複製到臨時目錄中。增加了兩條新線,現在它完美無缺!

希望這會幫助別人!我知道我花了很長時間尋找這個解決方案,並且找不到其他人試圖在可下載的報告中包含圖像。

library(shiny) 

# Define UI for application that draws a histogram 
ui<-shinyUI(fluidPage(
    titlePanel("Hello Shiny!"), 
    sidebarPanel(
     downloadButton('downloadReport',label="Download Report") 
    ), 
    mainPanel(
     p("Hello") 
    ) 
)) 

server<-shinyServer(function(input, output) { 
    output$downloadReport <- downloadHandler(
    filename = function() { 
     paste0('Report_.pdf') 
    }, 
    content = function(file) { 
     src <- normalizePath('report.rmd') 
     src2 <- normalizePath('smiley.png') #NEW 
     owd <- setwd(tempdir()) 
     on.exit(setwd(owd)) 
     file.copy(src, 'report.rmd') 
     file.copy(src2, 'smiley.png') #NEW 
     library(rmarkdown) 
     out <- render('report.rmd',pdf_document()) 
     file.rename(out, file) 
    } 
) 
}) 

shinyApp(ui, server)#Runs the app 
+0

我花了數小時試圖弄清楚這一點。謝謝@ sfeliz92的解決方案。 –

+0

你在哪裏把你的smiley.pgn文件保存在服務器上?在與app.r相同的目錄中?你如何在rmarkdown中調用圖像?它是'![](smiley.png)'? – Phil

+0

關於我以前的評論,這兩個問題的答案似乎都是「是」。 – Phil

相關問題