我很难在 Shiny 应用程序中导出 dygraph 的 PNG。我一直在按照示例使用此脚本并将其修改为 Shiny。我正在使用该库将我的 dygraph 对象传递给客户端。我遇到以下错误:shinyjs
未捕获的类型错误:无法在“CanvasRenderingContext2D”上执行“drawImage”:提供的值不是“(HTMLImageElement 或 HTMLVideoElement 或 HTMLCanvasElement 或 ImageBitmap)”类型
这是一个希望可重现的示例:
用户界面
library(shiny)
library(dygraphs)
library(shinyjs)
js.png <- '
shinyjs.exportPNG = function(params) {
var defaultParams = {
dygraph : null,
img : null,
userOptions : null,
}
params = shinyjs.getParams(params, defaultParams);
var image = document.getElementById(params.img);
Dygraph.Export.asPNG(params.dygraph, image);
}
'
shinyUI(fluidPage(
useShinyjs(),
extendShinyjs(text = js.png),
actionButton('btn', 'Go'),
includeScript('./www/dygraph-extra.js'),
dygraphOutput("plot"),
img(id = 'plot-static')
)
)
服务器.R
library(dplyr)
library(xts)
shinyServer(function(input, output) {
observeEvent(input$btn, {
js$exportPNG(dygraph = dyplot.global, img = 'plot-static')
})
plot.data <- reactive({
d <- airquality %>%
mutate(Date = as.Date(paste('1973', Month, Day, sep = '-'))) %>%
select(Date, Ozone, Temp) %>%
arrange(Date)
d.xts <- xts(d[,-1], order.by = d$Date)
})
output$plot <- renderDygraph({
g <- dygraph(plot.data()) %>%
dyRangeSelector()
dyplot.global <<- g
g
})
})
我对javascript非常陌生,所以请温柔。实际的情节和数据涉及更多,我非常投入到 dygraphs 中的交互和功能。谢谢你。