gpt4 book ai didi

r - Shiny - 动态地向 rmarkdown 报告添加部分

转载 作者:行者123 更新时间:2023-12-04 03:11:46 25 4
gpt4 key购买 nike

每次单击操作按钮 printNewPlot 时,我都想添加一个带有新标题和新图表的新报告部分。我该怎么做?

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))
```

最佳答案

您需要两件事:一个对象来存储您的输入列表;并在接收存储的输入作为参数的 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')
}

```

关于r - Shiny - 动态地向 rmarkdown 报告添加部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44713295/

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