从动态闪亮降价创建静态报告

我有一个闪亮的降价应用程序,其中我有几个数字,比如一周的不同日期.上面这些数字是一个文字区域,我写了一些评论.

我希望能够将此报告导出为静态降价格式.

我将在下面展示(主要)可重现的示例,第一部分是我想要编辑的代码,以便它在单独的文件中创建第二部分的代码.

---
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 urls更改为任何现有文件以使其可重现).

---
#  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密钥(我假设你这样做,因为你打算这样做).

---
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 = "")

```
点赞