【发布时间】: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