我有一个 shiny markdown
我有几个数字的应用程序,比如一周中的不同天数。在这些数字上方是一个文本区域,我在其中写了一些评论。
我希望能够将此报告导出到 static markdown
格式。
我在下面展示了一个(主要是)可重现的例子,第一部分是我想要编辑的代码,以便它在单独的文件中从第二部分创建代码。
---
title: "WEEKLY REPORT"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```
```{r header, echo=FALSE}
selectInput("x", label = "x",choices = 1:10,width="100%")
actionButton("button", "Export report")
```
## Monday
```{r monday}
textAreaInput("mon", label = NULL)
renderPlot({
plot(log(1:input$x))
})
```
## Tuesday
```{r tuesday}
textAreaInput("tue", label = NULL)
renderPlot({
plot(sin(1:input$x))
})
```
如何编辑它以便操作按钮创建一个新的 Rmd 文件,其中包含以下代码(或将创建类似输出的 Rmd 文件)? (将 png url 更改为任何现有文件以使其可重现)。
---
# title: "WEEKLY REPORT"
output: html_document
---
## Monday
The text I would have put on the first box
![](plot_monday.png)
## Tuesday
The text I would have put on the second box
![](plot_tuesday.png)
所以基本上输入选择器必须去,文本区域需要更改为标准文本(可能包含 Markdown ),并且必须将绘图导出为相关输入的图片文件,然后作为图片插入返回告中。
理想情况下,我还希望能够将星期一和星期二导出到不同的 Rmd 文件中。
最佳答案
我认为最简单的方法是使用报告模板并将输入作为参数传递。
因此,您在与 Shiny 报告相同的目录中创建了一个文件名为“sampleRmdReport.Rmd”的新报告,其内容如下:
---
title: "Weekly Report"
author: "Moody_Mudskipper"
date: '`r format(Sys.Date(),"%Y-%B-%d")`'
output: html_document
params:
mondayText: "holder"
tuesdayText: "holder"
x: "holder"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results = "asis")
```
# Monday
```{r}
cat(params$mondayText)
```
```{r}
plot(log(1:params$x))
```
# Tuesday
```{r}
cat(params$tuesdayText)
```
```{r}
plot(sin(1:params$x))
```
然后,将以下内容添加到您的 Shiny 报告中:
Download the weekly report:
```{r}
downloadHandler(
filename = function(){
paste0("weeklyReport_generated_"
, format(Sys.Date(), "%Y%b%d")
, ".html")
}
, content = function(file){
rmarkdown::render(input = "sampleRmdReport.Rmd"
, output_file = file
, params = list(mondayText = input$mon
, tuesdayText = input$tue
, x = input$x
))
}
, contentType = "text/html"
)
```
制作完整文件:
---
title: "WEEKLY REPORT"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```
```{r header}
selectInput("x", label = "x",choices = 1:10,width="100%")
```
Download the weekly report:
```{r}
downloadHandler(
filename = function(){
paste0("weeklyReport_generated_"
, format(Sys.Date(), "%Y%b%d")
, ".html")
}
, content = function(file){
rmarkdown::render(input = "sampleRmdReport.Rmd"
, output_file = file
, params = list(mondayText = input$mon
, tuesdayText = input$tue
, x = input$x
))
}
, contentType = "text/html"
)
```
## Monday
```{r monday}
textAreaInput("mon", label = NULL)
renderPlot({
plot(log(1:input$x))
})
```
## Tuesday
```{r tuesday}
textAreaInput("tue", label = NULL)
renderPlot({
plot(sin(1:input$x))
})
```
然后,单击“下载”按钮将生成报告并提示用户下载。请注意,如果您在 RStudio 中进行测试,文件名将不起作用。我建议在浏览器中打开它进行测试。
然后,如果您希望能够生成单独的每日报告,只需为您想要的报告添加一个模板,并为每个报告添加一个下载按钮。或者,您可以制作您的
downloadHandler
每天生成一份报告(从模板中),并将它们放在一个压缩目录中以供下载。(注意:我倾向于发现这在 Shiny App 中比在 Markdown 文档中更灵活,特别是因为它允许更多地控制下载按钮。根据您的用例,可能值得考虑将其作为一种方法。)
根据评论,这是一个可以上传到 Imgur 并以这种方式插入图像的版本。用这个替换另一个模板或添加第二个按钮。请注意,我没有使 imgur 上传功能工作,因为我没有 API key (我假设您有,因为您打算这样做)。
---
title: "Weekly Report"
author: "Moody_Mudskipper"
date: '`r format(Sys.Date(),"%Y-%B-%d")`'
output:
html_document:
self_contained: false
params:
mondayText: "holder"
tuesdayText: "holder"
x: 1
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results = "asis")
library(ggplot2)
tempDir <- tempdir()
uploadImgur <- function(fileName){
# This function would need to upload with the Imgur API
# I don't have a key, and don't want to set one up
# for this example.
# It would return the url that imgur assigns the image
# here, I am using a placeholder
outUrl <- "https://i.imgur.com/WsUV4DK.gif"
return(outUrl)
}
```
# Monday
```{r}
cat(params$mondayText)
```
```{r}
tempPlot <-
ggplot(mapping = aes(x = 1:params$x
, y = log(1:params$x))) +
geom_point() +
xlab("X") +
ylab("Y")
tempFile <- tempfile("plot_", tempDir, ".png")
ggsave(tempFile, tempPlot, width = 4, height = 4)
imgurURL <- uploadImgur(tempFile)
cat("![](", imgurURL,")", sep = "")
```
# Tuesday
```{r}
cat(params$tuesdayText)
```
```{r}
tempPlot <-
ggplot(mapping = aes(x = sin(1:params$x)
, y = log(1:params$x))) +
geom_point() +
xlab("X") +
ylab("Y")
tempFile <- tempfile("plot_", tempDir, ".png")
ggsave(tempFile, tempPlot, width = 4, height = 4)
imgurURL <- uploadImgur(tempFile)
cat("![](", imgurURL,")", sep = "")
```
关于r - 从动态 Shiny Markdown 创建静态报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515474/