为什么 R 指的是旧的 .Rmd 文件

why is R referring to old .Rmd file

我有一个可以运行的 shinyapp,我想包含一个 rmarkdown 文档,您可以在与我的 shinyapp 交互后下载该文档。

当我开始创建 .rmd 文件时,我对其进行了更改和编辑,但它仍然保存了旧版本。我知道这一点是因为我向未显示的 .rmd 文件添加了文本,但如果我将文档名称更改为 1.rmd 并在我的 R 代码中引用它,它会生成新文档。

ui

               radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                        inline = TRUE),
           downloadButton('downloadReport', 'Download Report'),

服务器

output$downloadReport <- downloadHandler(
filename = function() {
paste('report', sep = '.', switch(
  input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},

content = function(file) {
src <- normalizePath('shinyAppTest1.Rmd')

# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'shinyAppTest1.Rmd')

out <- rmarkdown::render('shinyAppTest1.Rmd', switch(
  input$format,
  PDF = pdf_document(fig_caption = TRUE, fig_width = 7, fig_height = 3.5),
  HTML = html_document(),
  Word = word_document()
))
file.rename(out, file)
}
)

rmd.

---
title: "Untitled"
output: pdf_document
---

This is an R Markdown document. Markdown is a simple formatting syntax for          authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

You can also embed plots, for example:

TEST

我认为错误是我没有告诉它覆盖那里的旧文件。

output$downloadReport <- downloadHandler(
 filename = function() {
paste('report', sep = '.', switch(
  input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},

content = function(file) {
src <- normalizePath('shinyAppTest1.Rmd')

# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'shinyAppTest1.Rmd', overwrite = TRUE)

out <- rmarkdown::render('shinyAppTest1.Rmd', switch(
  input$format,
  PDF = pdf_document(fig_caption = TRUE, fig_width = 7, fig_height = 3.5),
  HTML = html_document(),
  Word = word_document()
))
file.rename(out, file)
})