2017-08-28 159 views
0

失败,我有一个RMarkdown模板,template.Rmd[R闪亮/ R降价:渲染()内downloadHandler

--- 
title: "Template" 
output: tufte_handout 
params: 
    data: !r data.frame() 
--- 

```{r setup, include=FALSE} 
library(ggplot2) 
knitr::opts_chunk$set(echo = TRUE) 
``` 

# Title 

## Another Title 

```{r echo=FALSE} 
ggplot(data = params$data, mapping = aes(x=params$data$X, y=params$data$Y)) + 
    geom_point() 
``` 

那我也该R闪亮的应用,app.R

library(shiny) 
library(rmarkdown) 

data <- data.frame(X = 1:10, Y = 11:20) 

ui <- fluidPage(fluidRow(column(
    width = 6, 
    actionButton("actionButton", "PDF"), 
    downloadButton("downloadButton", "PDF") 
))) 

server <- function(input, output) { 
    observeEvent(input$actionButton, { 
    renderedFile <- render(
     input = "template.Rmd", 
     output_format = "tufte::tufte_handout", 
     params = list(data = data), 
     output_file = "output.pdf" 
    ) 
    browseURL(renderedFile) 


    }) 
    output$downloadButton <- 
    downloadHandler(filename <- "output.pdf", 
        content <- 
         function(file) { 
         renderedFile <- render(
          input = "template.Rmd", 
          output_format = "tufte::tufte_handout", 
          params = list(data = data), 
          output_file = "output.pdf" 
         ) 
         file.copy(renderedFile, file) 
         }) 
} 

shinyApp(ui = ui, server = server) 

还有一个actionButton和一个downloadButton。他们都应该做同样的事情,或多或少:呈现PDF(精确的Tufte讲义),并分别下载它。虽然browseURL在我的机器上运行示例时效果很好,但在“真实”服务器中运行应用程序时,我需要downloadHandler

actionButton作品完美,但downloadButton失败:

"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS template.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc7146d9cfc5.pdf --template "C:\Users\paedubucher\Documents\R\win-library\3.4\tufte\rmarkdown\templates\tufte_handout\resources\tufte-handout.tex" --highlight-style pygments --latex-engine pdflatex --variable "documentclass:tufte-handout" 
! Undefined control sequence. 
<argument> C:\temp 
        \RtmpAtAlbM \file 714614f62c3_files 
l.78 ...62c3_files/figure-latex/unnamed-chunk-1-1} 

pandoc.exe: Error producing PDF 
Warning: running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS template.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc7146d9cfc5.pdf --template "C:\Users\paedubucher\Documents\R\win-library\3.4\tufte\rmarkdown\templates\tufte_handout\resources\tufte-handout.tex" --highlight-style pygments --latex-engine pdflatex --variable "documentclass:tufte-handout"' had status 43 
Warning: Error in : pandoc document conversion failed with error 43 
Stack trace (innermost first): 
    53: pandoc_convert 
    52: convert 
    51: render 
    50: download$func [C:\Users\paedubucher\Documents\R\pdf-download/app.R#28] 
    1: runApp 
Error : pandoc document conversion failed with error 43 

编辑:现在有正确的错误消息。 Pandoc失败(错误43),但在actionButton上下文中运行时一切正常。

+0

嗯,我在哪里有一个因子?我的数据是数值型的:'data < - data.frame(X = 1:10,Y = 11:20)' –

+0

啊,对不起,当我去掉'output'常数时,我搞砸了代码...我会修复它! –

+0

没关系。我没有测试你的代码。根据错误发表评论。对不起,噪音 – akrun

回答

0

GitHub pointed out the problem友好的R Shiny伙计们。不应该以这种方式使用output_file。这只是中间的LaTeX文件。 PDF文件的位置从render返回。此文件只需要移动到file参数指向的位置:

output$downloadButton <- 
    downloadHandler(
     filename = "output.pdf", 
     content = 
     function(file) { 
      output <- render(
      input = "template.Rmd", 
      output_format = "tufte::tufte_handout", 
      params = list(data = data) 
     ) 
      file.copy(output, file) 
     } 
    )