英文:
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("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)
}
)
}
)
答案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 <- density(faithful$eruptions, adjust = params$bw_adjust)
plot(dens, col = "blue")
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(
"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, params = list(bw_adjust = input$bw_adjust))
}
)
}
)
英文:
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 <- density(faithful$eruptions, adjust = params$bw_adjust)
plot(dens, col = "blue")
```
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(
"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, params = list(bw_adjust = input$bw_adjust))
}
)
}
)
答案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("Static report generation activated!")
} else {
print("Dynamic shiny activated!")
sliderInput(
"bw_adjust",
label = "Bandwidth adjustment:",
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 <- 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论