2017-06-23 34 views
0

每次單擊actionButton 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()函數,所以通過一個錯誤。

對於應用程序:

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

因此,您需要一個存儲每個點擊參數的RMD構建器,然後在一個文檔中打印所有這些存儲的參數? –

+0

是的,這正是我想要的。 – andandandand