私はかなり長くて複雑な光沢のあるアプリケーションを作成しました。これはさまざまなユーザーの入力に基づいてテーブルとプロットを生成します。私は、アプリに現在表示されているチャートやプロットを表示する「レポートをダウンロード」ボタンを作成したいと思います。シャイニーのアプリケーションからマークダウンレポートを出力できません
しかし、私は動作するレポートを生成しているようには見えません。私は単純な解決策があることを望んで、私の問題を含んでいる光沢のあるアプリの例を使用しました。 「レポートをダウンロード」をクリックすると、保存場所を選択して「レポート」というレポートが作成されます。ただし、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マークダウンファイル:
---
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()
```
私はここに簡単な何かが欠けする必要があります
!
私はどれくらい時間を無駄にしたのか分かりません。私はそれが何か簡単だと分かっていました。ありがとうございました!!! – sym246
削除されたコメントに返信したようです。なにが問題だったの? –
問題を解決した回答を追加しました:) – sym246