我想从闪亮的应用程序导出html / pdf文件,我发现了这个 https://shiny.rstudio.com/articles/generating-reports.html
然后我尝试使用本网站上显示的代码:
library(shiny)
server <- function(input, output) {
output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "report.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(n = input$slider)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)})}
ui <- fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
downloadButton("report", "Generate report"))
shinyApp(ui = ui, server = server)
但是,我无法下载报告(Failed-Server问题)并收到错误:
Listening on http://127.0.0.1:3638
Warning in normalizePath(path, winslash = winslash, mustWork = mustWork) :
path[1]="/tmp/Rtmp1BbjoR/test.Rmd": No such file or directory
Warning: Error in tools::file_path_as_absolute: file '/tmp/Rtmp1BbjoR/test.Rmd' does not exist
Stack trace (innermost first):
58: tools::file_path_as_absolute
57: dirname
56: setwd
55: rmarkdown::render
54: download$func [example_code/report.R#23]
5: <Anonymous>
4: do.call
3: print.shiny.appobj
2: print
1: print
Error : file '/tmp/Rtmp1BbjoR/test.Rmd' does not exist
我正在研究R服务器。这个错误有什么建议吗?
提前致谢。
答案 0 :(得分:0)
要在网站上重新创建示例,请使用骨架.Rmd
(名为report.Rmd
,最好创建为R
脚本,以避免不可见的.txt
分机。)文件需求出现在您的工作目录中。此shell文件的内容在问题中提到的网页上给出。所以步骤是:
使用声明的网页创建文件report.Rmd
。请确保它没有.txt
扩展名。
将其放在R会话的工作目录中。
按原样运行代码。