无法在ShinyApp中生成R-Markdown报告

时间:2019-01-31 11:54:01

标签: r shiny rstudio r-markdown

我创建了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()
  })
})
```

1 个答案:

答案 0 :(得分:0)

好的,我终于明白了!首先,我们需要使用的功能“运行外部”运行shinyApp。其次,我们不需要那些乱七八糟我在模板制作。简单:

---
title: "Untitled"
author: "user"
date: "date"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(ggplot2)
library(lattice)
library(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()
)
})