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

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

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]"
  1. ---title: Report
  2. runtime: shiny
  3. output: html_document
  4. ---
  5. # 1. Here the report.rmd
  6. sliderInput("bw_adjust", label = "Bandwidth adjustment:",
  7. min = 0.2, max = 2, value = 1, step = 0.2)
  8. renderPlot({
  9. dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  10. plot(dens, col = "blue")
  11. })
  1. # 2. And here my app
  2. library(shiny)
  3. shinyApp(
  4. ui = fluidPage(
  5. sliderInput(
  6. "bw_adjust",
  7. label = "Bandwidth adjustment:",
  8. min = 0.2,
  9. max = 2,
  10. value = 1,
  11. step = 0.2
  12. ),
  13. downloadButton("report", "Generate report")
  14. ),
  15. server = function(input, output) {
  16. output$report <- downloadHandler(
  17. # For PDF output, change this to "report.pdf",
  18. filename = "report.html",
  19. content = function(file) {
  20. rmarkdown::render("report.Rmd", output_file = file)
  21. }
  22. )
  23. }
  24. )

希望这对您有帮助。

英文:

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]"
  1. ---title: Report
  2. runtime: shiny
  3. output: html_document
  4. ---
  5. # 1. Here the report.rmd
  6. sliderInput(&quot;bw_adjust&quot;, label = &quot;Bandwidth adjustment:&quot;,
  7. min = 0.2, max = 2, value = 1, step = 0.2)
  8. renderPlot({
  9. dens &lt;- density(faithful$eruptions, adjust = input$bw_adjust)
  10. plot(dens, col = &quot;blue&quot;)
  11. })
  1. # 2. And here my app
  2. library(shiny)
  3. shinyApp(
  4. ui = fluidPage(
  5. sliderInput(
  6. &quot;bw_adjust&quot;,
  7. label = &quot;Bandwidth adjustment:&quot;,
  8. min = 0.2,
  9. max = 2,
  10. value = 1,
  11. step = 0.2
  12. ),
  13. downloadButton(&quot;report&quot;, &quot;Generate report&quot;)
  14. ),
  15. server = function(input, output) {
  16. output$report &lt;- downloadHandler(
  17. # For PDF output, change this to &quot;report.pdf&quot;
  18. filename = &quot;report.html&quot;,
  19. content = function(file) {
  20. rmarkdown::render(&quot;report.Rmd&quot;, output_file = file)
  21. }
  22. )
  23. }
  24. )

答案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:

  1. ---
  2. title: Report
  3. runtime: shiny
  4. output: html_document
  5. params:
  6. bw_adjust: 1
  7. ---
  1. dens &lt;- density(faithful$eruptions, adjust = params$bw_adjust)
  2. 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:

  1. library(shiny)
  2. shinyApp(
  3. ui = fluidPage(
  4. sliderInput(
  5. &quot;bw_adjust&quot;,
  6. label = &quot;Bandwidth adjustment:&quot;,
  7. min = 0.2,
  8. max = 2,
  9. value = 1,
  10. step = 0.2
  11. ),
  12. downloadButton(&quot;report&quot;, &quot;Generate report&quot;)
  13. ),
  14. server = function(input, output) {
  15. output$report &lt;- downloadHandler(
  16. # For PDF output, change this to &quot;report.pdf&quot;
  17. filename = &quot;report.html&quot;,
  18. content = function(file) {
  19. rmarkdown::render(&quot;report.Rmd&quot;, output_file = file, params = list(bw_adjust = input$bw_adjust))
  20. }
  21. )
  22. }
  23. )

如何根据包含响应式图表的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:

  1. ---
  2. title: Report
  3. runtime: shiny
  4. output: html_document
  5. params:
  6. bw_adjust: 1
  7. ---
  8. ```{r}
  9. dens &lt;- density(faithful$eruptions, adjust = params$bw_adjust)
  10. plot(dens, col = &quot;blue&quot;)
  11. ```

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:

  1. library(shiny)
  2. shinyApp(
  3. ui = fluidPage(
  4. sliderInput(
  5. &quot;bw_adjust&quot;,
  6. label = &quot;Bandwidth adjustment:&quot;,
  7. min = 0.2,
  8. max = 2,
  9. value = 1,
  10. step = 0.2
  11. ),
  12. downloadButton(&quot;report&quot;, &quot;Generate report&quot;)
  13. ),
  14. server = function(input, output) {
  15. output$report &lt;- downloadHandler(
  16. # For PDF output, change this to &quot;report.pdf&quot;
  17. filename = &quot;report.html&quot;,
  18. content = function(file) {
  19. rmarkdown::render(&quot;report.Rmd&quot;, output_file = file, params = list(bw_adjust = input$bw_adjust))
  20. }
  21. )
  22. }
  23. )

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

答案2

得分: 0

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

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

  1. ---
  2. title: 报告
  3. runtime: shiny
  4. output: html_document
  5. params:
  6. bw_adjust: 1
  7. ---
  8. # 根据interactive()函数的结果,在report.rmd中有条件地生成小部件
  9. ```{r}
  10. if (interactive()) {
  11. print("激活静态报告生成!")
  12. } else {
  13. print("激活动态shiny!")
  14. sliderInput(
  15. "bw_adjust",
  16. label = "带宽调整:",
  17. min = 0.2,
  18. max = 2,
  19. value = 1,
  20. step = 0.2)
  21. }

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

  1. if (interactive()) {
  2. dens <- density(faithful$eruptions, adjust = params$bw_adjust)
  3. plot(dens, col = "blue")
  4. } else {
  5. renderPlot({
  6. dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  7. plot(dens, col = "green")
  8. })
  9. }
  1. <details>
  2. <summary>英文:</summary>
  3. Your solution allows producing the report.html from report.rmd via the shiny app.R. However it deletes the reactive plot from report.rmd.
  4. I suggest to complete the code in report.rmd
  5. ---
  6. title: Report
  7. runtime: shiny
  8. output: html_document
  9. params:
  10. bw_adjust: 1
  11. ---
  12. # Conditional production of a widget in report.rmd
  13. # depending on the function interactive()
  14. ```{r}
  15. if(interactive()) {
  16. print(&quot;Static report generation activated!&quot;)
  17. } else {
  18. print(&quot;Dynamic shiny activated!&quot;)
  19. sliderInput(
  20. &quot;bw_adjust&quot;,
  21. label = &quot;Bandwidth adjustment:&quot;,
  22. min = 0.2,
  23. max = 2,
  24. value = 1,
  25. step = 0.2)
  26. }
  27. ```
  28. # Conditional production of a plot, with params or reactive input
  29. # depending on the function interactive()
  30. ```{r}
  31. if (interactive()) {
  32. dens &lt;- density(faithful$eruptions, adjust = params$bw_adjust)
  33. plot(dens, col = &quot;blue&quot;)
  34. } else {
  35. renderPlot({
  36. dens &lt;- density(faithful$eruptions, adjust = input$bw_adjust)
  37. plot(dens, col = &quot;green&quot;)
  38. })
  39. }
  40. ```
  41. </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:

确定