我从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,非常感谢任何建议!
UI:
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)
答案 0 :(得分:0)
我发现在工作目录中放置一个虚拟的“ report.Rmd”文件使我能够删除上面的隐秘错误消息。
不幸的是,这在我的终端上生成了一些新的错误消息,但仍然无法使我下载PDF,但确实使我能够跳过OP中报告的原始错误消息。