如何将一个情节(renderPlot)从闪亮的app作为参数传递给R Markdown?

问题描述 投票:1回答:1

我正在尝试使用R Markdown下载报表闪亮的应用程序,但我输了!我需要将一个情节从闪亮的参数传递到R Markdown,然后在我的报告中包含这个情节。我搜索了很多关于这个,但我找不到任何东西。我怎样才能在报告中对此进行绘制?

Server.R

lm_dif_filter <- reactive({
    lm_dif_corn[(lm_dif_corn$farmer == input$farmer) & (lm_dif_corn$Treat_X == 'Farmer'),]
  })

output$difPlot <- renderPlotly({
      dif <- ggplot(data=lm_dif_filter(), aes(x=Treat_Y, y=dif)) +
             geom_bar(stat="identity",color = 'black', position=position_dodge(), width = 0.7)+
             geom_hline(yintercept = 0) + 
             #annotate("text", min(Treat_Y), 0, vjust = -1, label = "Farmer")+
             theme(legend.position = "none") +
             labs(x = "Treats", y = "Diff")

      ggplotly(dif)

去下载:

output$report <- downloadHandler(
     filename = "report.pdf",
     content = function(file) {
       tempReport <- file.path(tempdir(), "report.Rmd")
       file.copy("report.Rmd", tempReport, overwrite = TRUE)

       # Set up parameters to pass to Rmd document
       params <- list(set_subtitle = input$farmer, plot =  output$difPlot)
       rmarkdown::render(tempReport, output_file = file,
                         params = params,
                         envir = new.env(parent = globalenv())
       )
     }
   )

我的report.rmd

---
title: "Some title"
params:
 set_subtitle: test
 plot: NA
subtitle: "`r params$set_subtitle`"
date: '`r format(Sys.Date(), "%B %d, %Y")`'

output:
  pdf_document:
    toc: yes 
header-includes:
    - \usepackage{fancyhdr}
always_allow_html: yes
---
\addtolength{\headheight}{1.0cm} 
\pagestyle{fancyplain} 
\lhead{\includegraphics[height=1.2cm]{bg.png}} 
\renewcommand{\headrulewidth}{0pt} 


```{r, include=FALSE}
options(tinytex.verbose = TRUE)
knitr::opts_chunk$set(echo = FALSE)
cat(params$plot)

variables parameters shiny r-markdown
1个回答
1
投票

一个简单的选择是不传递绘图,而是传递参数,并参考闪亮的应用程序和Rmd doc使用的共享绘图函数。例如,

闪亮的应用程序,

注意source("util.R")report_hist(params$n)



source("util.R")
library(shiny)
shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report"),
    plotOutput("report_hist")
  ),
  server = function(input, output) {
    output$report_hist <- renderPlot({
      report_hist(n = input$slider)
    })
    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())
        )
      }
    )
  }
)

Rmd报告,

注意report_hist(params$n)

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

```{r}

# The `params` object is available in the document.
params$n
```

A plot of `params$n` random points.

```{r}

report_hist(params$n) #note this function was created in util.R and loaded by the shiny app. 

```

util.R中的共享功能

report_hist <- function(n){
  hist(rnorm(n))
}

这是一个演示闪亮的应用程序,你可以测试它,https://rstudio.cloud/project/295626

© www.soinside.com 2019 - 2024. All rights reserved.