R-生成PDF报告

R-生成PDF报告,r,pdf,shiny,r-markdown,R,Pdf,Shiny,R Markdown,我需要从一个闪亮的应用程序生成并下载PDF报告。我找到了这个(我将app.R和report.Rmd文件放在同一个文件夹中): 一方面,我认为问题在于我不在正确的文件夹中,另一方面,我无法访问tempDir 有什么想法吗?是否存在tempReport子目录?尝试使用dir.create预先创建它。我认为tempdir()的思想实际上是允许在单独的临时文件夹中创建PDF文件,然后运行并删除。我尝试先创建子目录,但没有帮助。我无法在我的计算机上复制错误。但是我在我的机器上有完全的写权限。也许写权限仍

我需要从一个闪亮的应用程序生成并下载PDF报告。我找到了这个(我将app.R和report.Rmd文件放在同一个文件夹中):

一方面,我认为问题在于我不在正确的文件夹中,另一方面,我无法访问tempDir


有什么想法吗?

是否存在
tempReport
子目录?尝试使用
dir.create
预先创建它。我认为tempdir()的思想实际上是允许在单独的临时文件夹中创建PDF文件,然后运行并删除。我尝试先创建子目录,但没有帮助。我无法在我的计算机上复制错误。但是我在我的机器上有完全的写权限。也许写权限仍然是你的问题?也许
tempdir()
失败了。
tempdir
只为您提供了放置临时目录的位置,而没有创建临时目录。请在复制之前尝试
dir.exists
,以确定。我迟到了,非常感谢大家!Sebastian-c如果您愿意,您可以将您的评论作为答案发布,我会接受。是否存在
tempReport
子目录?尝试使用
dir.create
预先创建它。我认为tempdir()的思想实际上是允许在单独的临时文件夹中创建PDF文件,然后运行并删除。我尝试先创建子目录,但没有帮助。我无法在我的计算机上复制错误。但是我在我的机器上有完全的写权限。也许写权限仍然是你的问题?也许
tempdir()
失败了。
tempdir
只为您提供了放置临时目录的位置,而没有创建临时目录。请在复制之前尝试
dir.exists
,以确定。我迟到了,非常感谢大家!Sebastian-c如果你愿意,你可以把你的评论作为回答,我会接受的。
shinyApp(
ui = fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
downloadButton("report", "Generate report")
),
server = function(input, output) {
output$report <- downloadHandler(
  # For PDF output, change this to "report.pdf"
  filename = "report.html",
  content = function(file) {
    # Copy the report file to a temporary directory before processing it, in
    # case we don't have write permissions to the current working dir (which
    # can happen when deployed).
    tempReport <- file.path(tempdir(), "report.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = TRUE)

    # Set up parameters to pass to Rmd document
    params <- list(n = input$slider)

    # Knit the document, passing in the `params` list, and eval it in a
    # child of the global environment (this isolates the code in the document
    # from the code in this app).
    rmarkdown::render(tempReport, output_file = file,
      params = params,
      envir = new.env(parent = globalenv())
    )
  }
)
}
)

report.Rmd:

---
title: "Dynamic report"
output: html_document
params:
n: NA
---

```{r}
# The `params` object is available in the document.
params$n
```

A plot of `params$n` random points.
Warning in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="C:\Users\*\AppData\Local\Temp\Rtmp0SaXzj\report.Rmd": Das      System kann die angegebene Datei nicht finden
Warning: Error in tools::file_path_as_absolute: file 'C:\Users\*\AppData\Local\Temp\Rtmp0SaXzj\report.Rmd' does not exist
Stack trace (innermost first):
57: tools::file_path_as_absolute
56: dirname
55: setwd
54: rmarkdown::render
53: download$func [#255]
 4: <Anonymous>
 3: do.call
 2: print.shiny.appobj
 1: <Promise>
Error : file 'C:\Users\*\AppData\Local\Temp\Rtmp0SaXzj\report.Rmd' does not exist
file.copy("report.Rmd", "/test/report.Rmd")
>FALSE