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

时间:2016-10-04 15:00:32

标签: r shiny rstudio r-markdown

我有一个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

1 个答案:

答案 0 :(得分:0)

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

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)
})