【问题标题】:rmarkdown normalizePath error in downloadrmarkdown normalizePath 下载错误
【发布时间】:2018-07-23 20:59:38
【问题描述】:

我从https://github.com/rstudio/shiny-examples/tree/master/016-knitr-pdf 获得了以下代码,但是无法让它在我的计算机上运行。 PDF、html 和 word 都会导致相同的错误 - 在我浏览器的下载窗口中,它只是显示“失败 - 服务器问题”,在我的 r 控制台中,它为我提供了以下详细信息:

Warning in normalizePath(path.expand(path), winslash, mustWork) :
 path[1]="report.Rmd": The system cannot find the file specified
Warning in normalizePath(path.expand(path), winslash, mustWork) :
 path[1]="report.Rmd": The system cannot find the file specified
Warning: Error in tools::file_path_as_absolute: file 'report.Rmd' does not 
exist
 [No stack trace available]

任何想法如何解决这个错误?我从未使用过 rmarkdown,非常感谢任何建议!

用户界面:

library(shiny)

ui <- fluidPage(
   title = 'Download a PDF report',
   sidebarLayout(
   sidebarPanel(
   helpText(),
  selectInput('x', 'Build a regression model of mpg against:',
              choices = names(mtcars)[-1]),
  radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
               inline = TRUE),
  downloadButton('downloadReport')
),
   mainPanel(
     plotOutput('regPlot')
   )
 )

)

服务器:

 server <- function(input, output) {

 regFormula <- reactive({
   as.formula(paste('mpg ~', input$x))
 })

 output$regPlot <- renderPlot({
   par(mar = c(4, 4, .1, .1))
   plot(regFormula(), data = mtcars, pch = 19)
 })

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

   content = function(file) {
     src <- normalizePath('report.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, 'report.Rmd', overwrite = TRUE)

     library(rmarkdown)
     out <- render('report.Rmd', switch(
       input$format,
       PDF = pdf_document(), HTML = html_document(), Word = word_document()
     ))
     file.rename(out, file)
   }
   )

  }

  shinyApp(ui = ui, server = server)

【问题讨论】:

  • 你把你的文件report.Rmd放在哪里了?
  • 我有一个空白报告。Rmd 保存在我的桌面上。我是否需要在其中指定路径? @StéphaneLaurent
  • 当然需要指定路径。

标签: r shiny r-markdown


【解决方案1】:

我发现在我的工作目录中放置一个虚拟的“report.Rmd”文件可以让我删除上面的神秘错误消息。

遗憾的是,这在我的终端上生成了一些新的错误消息,它仍然无法让我下载 PDF,但它确实让我能够超越 OP 中报告的原始错误消息。

【讨论】:

    猜你喜欢
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2017-05-27
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多