繁体   English   中英

无法从Shiny应用程序输出降价报告

[英]Cannot output markdown report from Shiny app

我创建了一个相当长且复杂的闪亮应用程序,该应用程序根据各种用户输入生成表格和图表。 我想创建一个“下载报告”按钮,该按钮将显示当前在应用程序上可见的图表和绘图。

但是,我似乎无法提供有效的报告。 我使用了一个包含问题的闪亮示例应用程序,希望有一个简单的解决方案。 当我单击“下载报告”时,它要求我选择保存位置并生成一个名为“报告”的报告。 但是,它不是HTML格式。 它实际上没有任何格式,因此我无法打开它并查看结果

闪亮的应用程序:

#install.packages("shiny")
library(shiny)
library(ggplot2)

ui <- fluidPage(
  title = 'Example application',
  sidebarLayout(
    sidebarPanel(
      helpText(),
      selectInput('x', 'Build a regression model of mpg against:',
                  choices = names(mtcars)[-1]),
      radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                   inline = TRUE),
      downloadButton('downloadReport')
    ),
    mainPanel(
      plotOutput('regPlot')
    )
  )
)


server <- function(input, output) {

  chart1 <- reactive({
    ggplot(data = mtcars, aes(x=input$x, y=mpg))+geom_point()
  })

  output$regPlot <- renderPlot({
    chart1()
  })

  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(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)

      library(rmarkdown)
      out <- render('report.Rmd', switch(
        input$format,
        PDF = pdf_document(), HTML = html_document(), Word = word_document()
      ))
      file.rename(out, file)
    }
  )

}

shinyApp(ui=ui, server=server)

R Markdown文件:

---
title: "Download report"
author: "Test"
date: "24 October 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(shiny)
library(rmarkdown)
```


## Output plot

Should output plot, here:

```{r test plot, echo=FALSE}
chart1()
```

我一定在这里缺少一些简单的东西!

该解决方案非常简单,但可能会对其他人有所帮助。

使用时,我的Shiny应用程序的默认设置为“在窗口中运行”。 但是,只需将其更改为“运行外部”,便可以根据需要下载报告。

希望这对某人有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM