2016-05-20 9 views
2

レポートをダウンロードできる光沢のあるアプリを作成したいと思います。今、私はスーパーシンプルなものを維持しようとしていると、光沢のあるアプリの唯一の入力は、ユーザがtextareaを入力使用することができますいくつかのテキストです:ShinyアプリとRmarkdownでレポートを生成

library(shiny) 
server <- function(input, output) { 
    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') 

     # temporarily switch to the temp dir, in case you do not have write 
     # permission to the current working directory 
     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) 
    } 
) 
} 

ui <- fluidPage(
    tags$textarea(id="text", rows=10, cols=80, "Default value"), 

    flowLayout(radioButtons('format', 'Document format', c('HTML', 'Word'), 
          inline = TRUE), 
      downloadButton('downloadReport')) 

) 

shinyApp(ui = ui, server = server) 

report.Rmdファイルは2つのだけの行があります

# Title 

`r renderPrint({ input$text })` 

悲しいですが、報告書にはinput$textが印刷されていません。後でこれを読んものに言い換える

答えて

1
library(shiny) 
server <- function(input, output) { 
    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') 

     # temporarily switch to the temp dir, in case you do not have write 
     # permission to the current working directory 
     owd <- setwd(tempdir()) 
     on.exit(setwd(owd)) 
     file.copy(src, 'report.Rmd', overwrite = TRUE) 

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

ui <- fluidPage(
    tags$textarea(id="text", rows=20, cols=155, 
       placeholder="Some placeholder text"), 

    flowLayout(radioButtons('format', 'Document format', c('HTML', 'Word'), 
          inline = TRUE), 
      downloadButton('downloadReport')) 

) 

shinyApp(ui = ui, server = server) 

--- 
title: "Parameterized Report for Shiny" 
output: html_document 
params: 
    text: 'NULL' 
--- 

# Some title 

`r params[["text"]]` 
+0

report.Rmdは... - あなたはにレンダリングされますあなたのRMDのYAMLヘッダに「のparams」セクションを追加する必要があります。 @ベンジャミンの[最後の解答ノート](http://stackoverflow.com/a/37072085/4606130)を参照してください。 – micstr

関連する問題