2017-06-23 33 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