Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从动态标记创建静态报告_R_Shiny_R Markdown - Fatal编程技术网

R 从动态标记创建静态报告

R 从动态标记创建静态报告,r,shiny,r-markdown,R,Shiny,R Markdown,我有一个闪亮的降价应用程序,其中有几个数字,比如一周中不同的几天。在这些图的上面是一个文本区域,我在这里写了一些评论 我希望能够将此报告导出为静态标记格式 我在下面展示一个(主要)可复制的示例,第一部分是我想要编辑的代码,这样它就可以在一个单独的文件中从第二部分创建代码 --- title: "WEEKLY REPORT" runtime: shiny output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk

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

我希望能够将此报告导出为
静态标记
格式

我在下面展示一个(主要)可复制的示例,第一部分是我想要编辑的代码,这样它就可以在一个单独的文件中从第二部分创建代码

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

因此,基本上输入选择器必须关闭,文本区域需要更改为标准文本(可能包含标记),绘图必须导出为相关输入的图片文件,然后作为图片插入报告中


理想情况下,我还希望能够将星期一和星期二导出到不同的Rmd文件中。

如果您可以使用
ggplot
将绘图保存为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中使用下载处理程序创建一个正确的下载按钮,因此这只在从RStudio运行时有效,而不是在浏览器中运行。

我认为最简单的方法是使用报告模板并将输入作为参数传递

因此,您可以在与您的Shining report相同的目录中创建一个文件名为“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))
```
然后,将以下内容添加到您的报告中:

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
为每天生成一份报告(来自模板),并将它们放在压缩目录中一起下载

(注意:我倾向于在闪亮的应用程序中比在降价文档中更灵活,尤其是在允许更多地控制下载按钮的情况下。根据您的使用情况,这可能值得考虑作为一种方法。)

根据这些评论,这里有一个版本可以上传到Imgur并以这种方式插入图像。用此模板替换其他模板或添加第二个按钮。请注意,我没有使imgur upload函数工作,因为我没有API密钥(我假设您有,因为您计划这样做)

---
标题:“每周报告”
作者:“穆迪•穆迪船长”
日期:''r格式(Sys.date(),%Y-%B-%d)''
输出:
html_文件:
自给自足:错误
参数:
星期一:“持有者”
星期二正文:“持有者”
x:1
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=FALSE,results=“asis”)
图书馆(GG2)

tempDir有一个软件包,可以让你从闪亮的应用程序中制作
png
屏幕截图。您可以通过将
Rmd
文档包装到一个闪亮的应用程序中来使用它。请看。我喜欢这种方法,在某些情况下它可能会令人满意,但有几个问题。首先,它给了我html,而不是将文件标记为输出,这意味着当结构已经在动态报告中列出时,我必须构建模板。这是可以接受的,因为我不会构建非常复杂的报告,但这仍然会增加错误和调试的时间和风险。我认为它可以通过一些动态报告的解析和一些正则表达式的乐趣来处理。为了透明,我想使用
wordpress
软件包从R开始写博客,使用名为
knit2wp
的函数,但它不适用于绘图(因此,对于此用途,我必须插入一个步骤,将PNG上传到imgur,并使用此url,但这是另一个问题)@Nice发布了一个带有
ggsave
的解决方案,我认为这可能与您的方法混合在一起,尽管出于某种原因,他很快删除了它。我听到了,如果它不起作用,它就不起作用。然而,对于您的第一个问题,我认为构建一个单独的模板实际上可能更好。它提供了构建稍有不同的内容的灵活性(如果您需要的话),这意味着您不必拘泥于闪亮的报告格式(例如,如果您想切换到应用程序以获得选项卡或侧面板,或者向报告添加动态分段等)。其次,您可以将imgur步骤添加到模板中。例如,使用ggsave,上传到imgur,然后使用
cat
将url(而不是绘图)动态添加到报告中。这两个点都很好,我想我可以保存图片,上传它们,通过url呈现它们,如果我用正确的代码替换你的
下载处理程序
,一步完成整个过程。
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))
})
```
---
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 = "")

```