我有一个简单的shiny 应用程序,我显示了两个图,一个是ggplot,另一个是dygraph.此外,我有一个按钮来生成图作为html输出.ggplot在html输出中呈现完美,但dygraph没有呈现,而是帮助dygraph生成的数据正在显示.

如何在html输出中生成dygraph

请考虑下面的代码,一个是app. r,另一个是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

而不是html输出中的dygraph,它显示如下所示:

enter image description here

推荐答案

你还应该渲染dygraph,只把它放在reactive是不够的.有可以使用的内置功能renderDygraph.

plot_dy <- reactive({
  dygraph(nhtemp, main = "New Haven Temperatures", ylab = "Temp (F)") 
})
  
output$plotDy <- renderDygraph({
  plot_dy()
})

也包括UI:

dygraphOutput("plotDy")

enter image description here

完整的代码示例:

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)

R相关问答推荐

geom_raster不适用于x比例中超过2,15的值

使用sensemakr和fixest feols模型(R)

如何使用shinyChatR包配置聊天机器人

将复杂的组合列表转换为数据框架

如何在xyplot中 for each 面板打印R^2

修改用R编写的用户定义函数

删除具有相同标题的tabPanel(shinly)

当我们有多个特殊字符时,使用gsub删除名称和代码'

为什么我的基准测试会随着样本量的增加而出现一些波动?

根据类别合并(汇总)某些行

LOF中的插图短文字幕

Select 季度月值

用两种 colored颜色 填充方框图

R-按最接近午夜的时间进行筛选

条形图顶部与其错误条形图不对齐

从多面条形图中删除可变部分

在使用具有Bray-Curtis相似性的pvCluust时计算p值

将具有坐标列表列的三角形转换为多个多边形

R/shiny APP:如何充分利用窗口?

我怎么才能把一盘棋变成一盘棋呢?