我有一个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 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文件中。
答案 0 :(得分:1)
我认为最简单的方法是使用报告模板并将输入作为参数传递。
因此,您在Shiny报告所在的目录中创建一个文件名为“sampleRmdReport.Rmd”的 new 报告,其中包含以下内容:
---
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 = "")
```
答案 1 :(得分:0)
如果您可以使用ggplot
,则可以使用ggsave
将图表保存在本地作为png。
你可以试试这个主文件:
---
title: "WEEKLY REPORT"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(ggplot2)
```
```{r header, echo=FALSE}
selectInput("x", label = "x",choices = 1:10,width="100%")
actionButton("button", "Export report")
observeEvent(input$button,{
params <- list(text = input$mon)
render('report.Rmd',params = params)
})
```
## Monday
```{r monday}
textAreaInput("mon", label = NULL)
renderPlot({
p <- ggplot(data.frame(x=1:input$x,y=log(1:input$x)),aes(x=x,y=y))+
geom_point()
ggsave("plot.png",plot=p)
p
})
```
对于您的静态报告(需要与另一个.Rmd位于同一文件夹中):
---
# title: "WEEKLY REPORT"
output: html_document
---
## Monday
```{r}
params$text
```
![plot](plot.png)
我不确定如何在闪亮的Rmd中使用下载处理程序制作一个正确的下载按钮,这样只有在从RStudio而不是在浏览器中运行时才有效。