r - 尝试从本地闪亮的应用程序下载报告时出现错误
问题描述
我有下面的闪亮应用程序,它可以生成和下载 pdf 和 word 格式的报告。要运行它,您需要将此处找到的这 2 个 rmd 文件保存在同一文件夹中,并安装tinytex。问题是,虽然文件应该正常下载,但实际上在浏览器中打开应用程序时会发生这种情况。当我在本地运行它时,pdf 只会打开文件,并且需要重命名文档,然后单击“保存”来获取它。为什么会这样?
library(shiny)
# devtools::install_github("jienagu/noteMD")
library(noteMD)
library(knitr)
library(rmarkdown)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
downloadButton('describe_download',"Download Report",class="butt" ),br(),
tags$head(tags$style(".butt{background-color:#230682;} .butt{color: #e6ebef;}")),
radioButtons('format', 'Document format', c('PDF', 'Word'),
inline = TRUE)
),
# Show a plot of the generated distribution
mainPanel(
fluidRow(
column(12,
helpText("Note: Any comments made in the box will be printed if you download the summary report.") ),
column(12,
tags$textarea(
"Please using any **markdown** syntax!",
id = 'markdowninput',
rows = 3,
style = 'width:100%;')) ),
helpText("Preview:"),
htmlOutput('htmlmarkdown')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$htmlmarkdown = reactive({
note_in_html(input$markdowninput)
})
output$describe_download = downloadHandler(
filename<- function(){
paste("Summary",Sys.Date(),switch(
input$format, PDF = '.pdf', Word = '.docx'
),sep = "")
},
content = function(file) {
if (input$format=="PDF"){
#### Progressing indicator
withProgress(message = 'Download in progress',
detail = 'This may take a while...', value = 0, {
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(0.01)
}
## End of progression
src <- normalizePath('summary_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, 'summary_report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('summary_report.Rmd', pdf_document())
file.rename(out, file)
})
### below is the end of pdf content
}else{
withProgress(message = 'Download in progress',
detail = 'This may take a while...', value = 0, {
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(0.01)
}
## End of progression
src <- normalizePath('summary_report_word.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, 'summary_report_word.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('summary_report_word.Rmd', word_document())
file.rename(out, file)
})
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
解决方案
推荐阅读
- c# - 组装和管理模块.Net
- laravel - Laravel如何在遇到重定向时进行测试预期状态码200但收到302
- python - 为什么下面的代码显示错误而不是全局范围?
- hardlink - 无法为 LXD 容器中挂载的主机文件建立硬链接
- css - 悬停动作移动文本。我如何解决它?
- c++ - 如何在另一个图像的开头关闭图像 - linux c ++
- .net - 如何仅将 Asp.Net Core Identity 与用户表一起使用
- angular - 离子滚动到虚拟滚动项目
- c# - 如何在 ComboBox (WindowsForm) 中启用 ctrl+c 快捷方式
- javascript - 我怎样才能按下元素键