【发布时间】:2019-06-24 21:08:55
【问题描述】:
我创建了 ShinyApp,一切正常。我想添加 downloadHandler 以生成包含所选图的 Markdown 报告。首先,我将文件上传到 ShinyApp。接下来,我使用 checkBoxInput 选择要绘制的变量。下一步是使用下拉列表在 Lattice/ggplot2 绘图之间进行选择,最后我想单击下载并获取它。 不幸的是,每次我尝试下载它时,我都会收到一个空白的 Markdown 页面。生成何种格式的报告并不重要。我想为此任务获得适当的逻辑。我尝试了在网络中找到的两种解决方案:
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')
owd <- setwd(getwd())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
})
和
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(getwd(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(graph = input$graph, colsel = input$colsel)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
因此,我分别为我的应用创建了 report.rmd 模板来填充它。我试图在里面放很多东西,但这些都不起作用。我错过了模板的逻辑吗?
---
title: "Untitled"
author: "user"
date: "date"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r plot, echo=TRUE}
plotdata <- reactive({
d <- dataIn()[, c(req(input$colsel))]
d <- melt(d, id.vars="Numer")
})
plot1 <- reactive({
dotplot(value~Numer, data=plotdata(), auto.key = list(space="right", title="Types"), groups=variable)
})
plot2 <- reactive({
ggplot(plotdata(), aes(x=Numer, y=value, color=variable)) +
geom_point()
})
graphInput <- reactive({
switch(input$graph,
"Lattice" = plot1(),
"ggplot2" = plot2())
})
renderPlot({
graphInput()
})
})
```
【问题讨论】:
标签: r shiny rstudio r-markdown