gpt4 book ai didi

variables - 如何将 Shiny 应用程序中的绘图(renderPlot)作为参数传递给 R Markdown?

转载 作者:行者123 更新时间:2023-12-04 15:46:31 24 4
gpt4 key购买 nike

我正在尝试使用 R Markdown 下载报告表单 shiny 应用程序,但我迷路了!我需要将 Shiny 的绘图作为参数传递给 R Markdown,然后将此绘图包含在我的报告中。我对此进行了很多搜索,但找不到任何东西。我如何在我的报告中绘制它?

服务器.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())
)
}
)

我的报告.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)

最佳答案

一个简单的选择是不传递绘图,而是传递参数,并引用 shiny 应用程序和 Rmd 文档使用的共享绘图函数。例如,

Shiny 的应用程序,

注意 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))
}

这是一个 Shiny 的演示应用程序,您可以使用它进行测试,https://rstudio.cloud/project/295626

关于variables - 如何将 Shiny 应用程序中的绘图(renderPlot)作为参数传递给 R Markdown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55516754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com