我已经创建了ShinyApp,其中一切都运行得很好。我想添加downloadHandler来生成包含所选图的Markdown报告。首先,我将一个文件上传到ShinyApp。接下来,我使用checkBoxInput选择要绘制的变量。下一步是使用下拉列表在Lattice/ggplot2图之间进行选择,最后我想单击download it并获取它。不幸的是,每次我尝试下载它时,我都会收到一个空白的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 =真)
```{r plot, echo=TRUE}plotdata <- reactive({
D <- dataIn(),c(请求(输入$colsel))
D <-熔体(d,id.vars=“数字”)
})
plot1 <-反应性({
点线图(value~Numer,data=plotdata(),auto.key = list(space="right",title="Types"),groups=variable)
})
plot2 <-反应性({
ggplot(plotdata(),aes(x=Numer,y=value,color=variable)) +
geom_point()})
graphInput <-反应性({
开关(输入$graph,
"Lattice" = plot1(), "ggplot2" = plot2())})
renderPlot({
graphInput()})
})
发布于 2019-01-31 21:10:25
好了,我终于明白了!首先,我们需要使用"Run External“函数来运行shinyApp。其次,我们不需要我在模板中弄得一团糟。简单:
---
title: "Untitled"
author: "user"
date: "date"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}knitr::opts_chunk$set(echo =真)
库(闪亮)
库(Ggplot2)
库(晶格)
库(Markdown)
```{r plot}plot1()
plot2()
graphInput()
其中plot1()、plot2()和graphinput()表示我的:
plot1 <- reactive({
dotplot(value~Numer,data=plotdata(), auto.key = list(space="right", title="WWW"), groups=variable)
})
plot2 <- reactive({
ggplot(plotdata(), aes(x=Numer, y=value, color=variable)) +
geom_point()
})
graphInput <- reactive({
switch(input$graph,
"Lattice" = plot1(),
"ggplot2" = plot2()
)
})https://stackoverflow.com/questions/54460025
复制相似问题