I have one simple shiny application where I am showing two plots, one is a ggplot
and another one is a dygraph
. Also I have one button to generate the plots as an html output. The ggplot
is rendering perfectly in the html output but the dygraph
is not rendering, rather the data which helps dygraph
to generate that is showing.
How can I also generate the dygraph
in the html output?
Please consider the below code, one is app.R and another one is report.Rmd
app.R
library(shiny)
library(ggplot2)
library(dygraphs)
report_path <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", report_path, overwrite = TRUE)
render_report <- function(input, output, params) {
rmarkdown::render(input,
output_file = output,
params = params,
envir = new.env(parent = globalenv())
)
}
ui <- fluidPage(
downloadButton("report", "Generate report"),
plotOutput("plot")
)
server <- function(input, output) {
plot_gg <- reactive({
ggplot(data = iris, aes(x=Sepal.Length, y=Petal.Length))+geom_point()
})
output$plot <- renderPlot({
plot_gg()
})
plot_dy <- reactive({
dygraph(nhtemp, main = "New Haven Temperatures", ylab = "Temp (F)")
})
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
params <- list(
gg_plot = plot_gg(),
dy_plot = plot_dy()
)
callr::r(
render_report,
list(input = report_path, output = file, params = params)
)
}
)
}
shinyApp(ui, server)
report.Rmd
---
title: "Dynamic report"
output: html_document
params:
gg_plot: NA
dy_plot: NA
---
{r}
params$gg_plot
{r}
params$dy_plot
Instead of the dygraph
in the html output it is showing something like below:
You also should render the dygraph
, putting it only into an reactive
is not sufficient. There is the built-in function renderDygraph
which can be used.
plot_dy <- reactive({
dygraph(nhtemp, main = "New Haven Temperatures", ylab = "Temp (F)")
})
output$plotDy <- renderDygraph({
plot_dy()
})
Also include the plot in the UI
:
dygraphOutput("plotDy")
Complete code example:
library(shiny)
library(dygraphs)
report_path <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", report_path, overwrite = TRUE)
writeLines(con = "report.Rmd", text = "---
title: 'Dynamic report'
output: html_document
params:
gg_plot: NA
dy_plot: NA
---
```{r plotlyout, echo=FALSE, message=FALSE, out.width='100%'}
params$gg_plot
params$dy_plot
```")
render_report <- function(input, output, params) {
rmarkdown::render(input,
output_file = output,
params = params,
envir = new.env(parent = globalenv())
)
}
ui <- fluidPage(
downloadButton("report", "Generate report"),
plotOutput("plot"),
dygraphOutput("plotDy")
)
server <- function(input, output) {
plot_gg <- reactive({
ggplot(data = iris, aes(x=Sepal.Length, y=Petal.Length))+geom_point()
})
output$plot <- renderPlot({
plot_gg()
})
plot_dy <- reactive({
dygraph(nhtemp, main = "New Haven Temperatures", ylab = "Temp (F)")
})
output$plotDy <- renderDygraph({
plot_dy()
})
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
params <- list(
gg_plot = plot_gg(),
dy_plot = plot_dy()
)
callr::r(
render_report,
list(input = report_path, output = file, params = params)
)
}
)
}
shinyApp(ui, server)