如何根据包含响应式图表的Rmarkdown生成HTML报告?

huangapple go评论66阅读模式
英文:

How can I generate a html report based on a Rmarkdown including reactives plots?

问题

以下是您要翻译的内容:

"I've developed a Rmarkdown report (report.Rmd) with reactive plot inside (like this shiny.rstudio.com)

Now I would like to export the content as a static report (report.html) via a shiny App and by choosing the desired values to show on the plot.
(like this shiny.rstudio.com, with the difference that I don't have parameters but input selected in the widget (input$bw_adjust).

Problem:

  • With R version 4.2.3 (private PC): I can generate a report.html but there is not the plot.
  • With R version 4.0.4 (business PC): No output generated: "Error in : path for html_dependency not provided [No stack trace available]"
---title: Report
runtime: shiny
output: html_document
---

# 1. Here the report.rmd

sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)

renderPlot({
  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  plot(dens, col = "blue")
})
# 2. And here my app

library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput(
      "bw_adjust",
      label = "Bandwidth adjustment:",
      min = 0.2,
      max = 2,
      value = 1,
      step = 0.2
    ),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf",
      filename = "report.html",
      content = function(file) {
        rmarkdown::render("report.Rmd", output_file = file)
      }
    )
  }
)

希望这对您有帮助。

英文:

I've developed a Rmarkdown report (report.Rmd) with reactive plot inside (like this shiny.rstudio.com)

Now I would like to export the content as static report (report.html) via a shiny App and by choosing the desired values to show on the plot.
(like this shiny.rstudio.com, with the difference that I dont have parameters but input selected in the widget (input$bw_adjust).

Problem:

  • With R version 4.2.3 (private PC): I can generate a report.html but there is not the plot.
  • With R version 4.0.4 (business PC): No output generated: "Error in : path for html_dependency not provided [No stack trace available]"
---title: Report
runtime: shiny
output: html_document
---

# 1. Here the report.rmd

sliderInput(&quot;bw_adjust&quot;, label = &quot;Bandwidth adjustment:&quot;,
              min = 0.2, max = 2, value = 1, step = 0.2)


renderPlot({
  dens &lt;- density(faithful$eruptions, adjust = input$bw_adjust)
  plot(dens, col = &quot;blue&quot;)
})

# 2. And here my app

library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput(
      &quot;bw_adjust&quot;,
      label = &quot;Bandwidth adjustment:&quot;,
      min = 0.2,
      max = 2,
      value = 1,
      step = 0.2
    ),
    downloadButton(&quot;report&quot;, &quot;Generate report&quot;)
  ),
  server = function(input, output) {
    output$report &lt;- downloadHandler(
      # For PDF output, change this to &quot;report.pdf&quot;
      filename = &quot;report.html&quot;,
      content = function(file) {
        rmarkdown::render(&quot;report.Rmd&quot;, output_file = file)
      }
    )
  }
)

答案1

得分: 1

A Shiny document is basically a shiny app and can only be run from within R or using a Shiny server to serve the document. There is no such thing as a static shiny document. As a consequence you end up with an HTML document which includes e.g. an input and even a container for the plot but is otherwise not working and hence you will see nothing.

Instead, to create a static report use a parameterized Rmd as shown in the link you referenced:

---
title: Report
runtime: shiny
output: html_document
params:
  bw_adjust: 1
---
dens &lt;- density(faithful$eruptions, adjust = params$bw_adjust)
plot(dens, col = &quot;blue&quot;)

In your shiny app you could then pass the value chosen by the user to the Rmd report via the params argument of rmarkdown::render:

library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput(
      &quot;bw_adjust&quot;,
      label = &quot;Bandwidth adjustment:&quot;,
      min = 0.2,
      max = 2,
      value = 1,
      step = 0.2
    ),
    downloadButton(&quot;report&quot;, &quot;Generate report&quot;)
  ),
  server = function(input, output) {
    output$report &lt;- downloadHandler(
      # For PDF output, change this to &quot;report.pdf&quot;
      filename = &quot;report.html&quot;,
      content = function(file) {
        rmarkdown::render(&quot;report.Rmd&quot;, output_file = file, params = list(bw_adjust = input$bw_adjust))
      }
    )
  }
)

如何根据包含响应式图表的Rmarkdown生成HTML报告?

英文:

A Shiny document is basically a shiny app and can only be run from within R or using a Shiny server to serve the document. There is no such thing as a static shiny document. As a consequence you end up with an HTML document which includes e.g. an input and even a container for the plot but is otherwise not working and hence you will see nothing.

Instead, to create a static report use a parameterized Rmd as shown in the link you referenced:

---
title: Report
runtime: shiny
output: html_document
params:
  bw_adjust: 1
---

```{r}
dens &lt;- density(faithful$eruptions, adjust = params$bw_adjust)
plot(dens, col = &quot;blue&quot;)
```

In your shiny app you could then pass the value chosen by the user to the Rmd report via the params argument of rmarkdown::render:

library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput(
      &quot;bw_adjust&quot;,
      label = &quot;Bandwidth adjustment:&quot;,
      min = 0.2,
      max = 2,
      value = 1,
      step = 0.2
    ),
    downloadButton(&quot;report&quot;, &quot;Generate report&quot;)
  ),
  server = function(input, output) {
    output$report &lt;- downloadHandler(
      # For PDF output, change this to &quot;report.pdf&quot;
      filename = &quot;report.html&quot;,
      content = function(file) {
        rmarkdown::render(&quot;report.Rmd&quot;, output_file = file, params = list(bw_adjust = input$bw_adjust))
      }
    )
  }
)

如何根据包含响应式图表的Rmarkdown生成HTML报告?

答案2

得分: 0

您的解决方案允许通过shiny app.R从report.rmd生成report.html。然而,它会删除report.rmd中的反应式图。

我建议在report.rmd中完成代码

---
title: 报告
runtime: shiny
output: html_document
params:
  bw_adjust: 1
---

# 根据interactive()函数的结果,在report.rmd中有条件地生成小部件

```{r}
if (interactive()) {
    print("激活静态报告生成!")
} else {
    print("激活动态shiny!")
    sliderInput(
      "bw_adjust",
      label = "带宽调整:",
      min = 0.2,
      max = 2,
      value = 1,
      step = 0.2)
}

根据interactive()函数的结果,有条件地生成图表,根据参数或反应式输入进行调整

if (interactive()) {
  dens <- density(faithful$eruptions, adjust = params$bw_adjust)
  plot(dens, col = "blue")
} else {
  renderPlot({
    dens <- density(faithful$eruptions, adjust = input$bw_adjust)
    plot(dens, col = "green")
  })
}

<details>
<summary>英文:</summary>

Your solution allows producing the report.html from report.rmd via the shiny app.R. However it deletes the reactive plot from report.rmd.

I suggest to complete the code in report.rmd


    ---
    title: Report
    runtime: shiny
    output: html_document
    params:
      bw_adjust: 1
    ---
    
    # Conditional production of a widget in report.rmd
    # depending on the function interactive()
    
    ```{r}
    
    if(interactive()) {
        print(&quot;Static report generation activated!&quot;)
    } else {
      print(&quot;Dynamic shiny activated!&quot;)
        sliderInput(
        &quot;bw_adjust&quot;,
        label = &quot;Bandwidth adjustment:&quot;,
        min = 0.2,
        max = 2,
        value = 1,
        step = 0.2)
    }
    
    
    ```
    # Conditional production of a plot, with params or reactive input
    # depending on the function interactive()

    ```{r}
    
    if (interactive()) {
      dens &lt;- density(faithful$eruptions, adjust = params$bw_adjust)
      plot(dens, col = &quot;blue&quot;)
    } else {
      renderPlot({
        dens &lt;- density(faithful$eruptions, adjust = input$bw_adjust)
        plot(dens, col = &quot;green&quot;)
      })
    }
    ```


</details>



huangapple
  • 本文由 发表于 2023年4月20日 03:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定