无法从 Shiny 应用程序输出降价报告
Cannot output markdown report from Shiny app
我创建了一个相当长且复杂的闪亮应用程序,它根据各种用户输入生成表格和图表。我想创建一个 'download report' 按钮,该按钮将显示当前在应用程序上可见的图表和绘图。
但是,我似乎无法生成有效的报告。我使用了一个包含我的问题的闪亮应用程序示例,希望有一个简单的解决方案。当我单击 'download report' 时,它要求我 select 保存位置并生成一个名为 'report' 的报告。但是,它不是 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 应用程序的默认设置是 'Run in Window'。但是,只需将其更改为 'Run External' 即可让我根据需要下载报告。
希望这对某人有所帮助!
我创建了一个相当长且复杂的闪亮应用程序,它根据各种用户输入生成表格和图表。我想创建一个 'download report' 按钮,该按钮将显示当前在应用程序上可见的图表和绘图。
但是,我似乎无法生成有效的报告。我使用了一个包含我的问题的闪亮应用程序示例,希望有一个简单的解决方案。当我单击 'download report' 时,它要求我 select 保存位置并生成一个名为 'report' 的报告。但是,它不是 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 应用程序的默认设置是 'Run in Window'。但是,只需将其更改为 'Run External' 即可让我根据需要下载报告。
希望这对某人有所帮助!