2017-06-23 9 views
0

アクションボタンprintNewPlotをクリックするたびに、新しいタイトルと新しいグラフプロットを含む新しいレポートセクションを追加します。これどうやってするの?シャイニー - セクションをrmarkdownレポートに動的に追加する

app.R

library(igraph) 

shinyApp(
     ui = fluidPage(
     sliderInput("slider", "Slider", 1, 100, 50), 
     actionButton("printNewPlot", "Print new plot to report"), 
     downloadButton("report", "Download report") 
    ), 
     server = function(input, output) { 
     output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf" 
      filename = "report.html", 
      content = function(file) { 
      # Copy the report file to a temporary directory before processing it, in 
      # case we don't have write permissions to the current working dir (which 
      # can happen when deployed). 
      tempReport <- file.path(tempdir(), "report.Rmd") 
      file.copy("report.Rmd", tempReport, overwrite = TRUE) 

      # Set up parameters to pass to Rmd document 
      params <- list(n = input$slider) 

      # Knit the document, passing in the `params` list, and eval it in a 
      # child of the global environment (this isolates the code in the document 
      # from the code in this app). 
      rmarkdown::render(tempReport, output_file = file, 
           params = params, 
           envir = new.env(parent = globalenv()) 
      ) 
      } 
     ) 
     } 
    ) 

をreport.Rmd

--- 
title: "Dynamic report" 
output: html_document 
params: 
    n: NA 
--- 

# I want a new title each time that printNewPlot is clicked 

```{r} 

plot(make_ring(params$n)) 
``` 

# If printNewPlot is clicked again, a new title must appear here 

```{r} 
#alongside a new plot 
plot(make_ring(params$n)) 
``` 
+0

私はのための命名規則でアプリから変数を含めるためにあなたのparamsを延長しますタイトルを入力し、パラメータを使用して見出しのインラインRコードタイトルを記述します。 –

答えて

1

あなたは二つのものが必要です:入力のリストを格納するオブジェクトを。格納された入力をパラメータとして受け取るRMD内でのループ印刷が含まれます。私がmake_ring()関数を持っていなかったので、エラーが発生したことに注意してください。 Appの

server = function(input, output) { 

RV <- reactiveValues(Clicks=c()) 

observeEvent(input$slider, { 

    #create object for clicked polygon 
    click <- input$slider 
    RV$Clicks <-c(RV$Clicks,click) 
    print(unique(RV$Clicks)) 

}) 

output$report <- downloadHandler(
    # For PDF output, change this to "report.pdf" 
    filename = "report.html", 
    content = function(file) { 
    # Copy the report file to a temporary directory before processing it, in 
    # case we don't have write permissions to the current working dir (which 
    # can happen when deployed). 
    tempReport <- file.path("report.Rmd") 
    #file.copy("report.Rmd", tempReport, overwrite = TRUE) 

    # Set up parameters to pass to Rmd document 
    params <- list(n = RV$Clicks) 

    # Knit the document, passing in the `params` list, and eval it in a 
    # child of the global environment (this isolates the code in the document 
    # from the code in this app). 
    rmarkdown::render(tempReport, output_file = file, 
         params = params, 
         envir = new.env(parent = globalenv()) 
    ) 
    } 
) 

} )

RMDファイルの場合

--- 
title: "Dynamic report" 
output: html_document 
params: 
    n: NA 
--- 

```{r grouping_loop, include=TRUE, echo=FALSE, results='asis'} 

n <- params$n 

for (i in n){ 
    cat('\n') 
    cat("# ", i, " \n") 
    print(
    i 
) 
    cat('\n') 
    cat('\n') 
} 

``` 
+0

ありがとうございますが、これは私が欲しいものではありません。 printNewPlotをクリックするたびに、新しいタイトルと新しいプロットを生成する必要があります。ボタンを少なくとも4〜5回クリックすると、クリックごとにRmdに新しいタイトルと新しいグラフが表示されます。 – andandandand

+0

したがって、各クリックのパラメータを保存し、それらのすべてのパラメータを1つのドキュメントに印刷するRMD Builderが必要ですか? –

+0

はい、まさに私が欲しいものです。 – andandandand

関連する問題