图表JSRadar下载处理程序创建空png。

问题描述 投票:0回答:1

我想在我的shiny应用程序中创建一个下载按钮,以下载用chartJSRadar创建的反应图。我无法解决这个问题! 我已经在互联网上查阅了相关的文档,但我无法解决这个问题,一直收到一个空的png。按照建议(保存在一个闪亮的应用程序中的情节), https:/groups.google.comforum#!msgshiny-discussu7gwXc8_vyYIZK_o7b7I8gJ。 我建立了一个函数... 所以我的代码是一个例子代码。

ui. R:

library(radarchart)

shinyUI(pageWithSidebar(
  headerPanel('Radarchart Shiny Example'),
  sidebarPanel(
    checkboxGroupInput('selectedPeople', 'Who to include', 
                       names(radarchart::skills)[-1], selected="Rich")
  ),
  mainPanel(
    chartJSRadarOutput("plot1", width = "450", height = "300"), width = 7,
  radioButtons(inputId = "var3", label = "Select the file type", choices = list("png", "pdf")),
  downloadButton('downloadPlot', 'Download Plot')
  )
))

服务器.R


library(radarchart)

shinyServer(function(input, output) {
  output$plot1 <- renderChartJSRadar({

    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE)
  })

  plot2 <- function(){
    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE)
  }

  output$downloadPlot <- downloadHandler(
    filename = "Shinyplot.png",
    content = function(file) {
      png(file)
      plot2()
      print(plot2())
      dev.off()
  })    
})
r download chart.js shinyapps radar-chart
1个回答
1
投票

这里有一个JavaScript的方式,这应该是更快的比。webshot,我想。

library(shiny)
library(radarchart)
library(htmlwidgets) # to use the 'onRender' function

js <- c(
  "function(el, x){",
  "  $('#downloadPlot').on('click', function(){",
  "    // Clone the chart to add a background color.",
  "    var cloneCanvas = document.createElement('canvas');",
  "    cloneCanvas.width = el.width;",
  "    cloneCanvas.height = el.height;",
  "    var ctx = cloneCanvas.getContext('2d');",
  "    ctx.fillStyle = '#FFFFFF';",
  "    ctx.fillRect(0, 0, el.width, el.height);",
  "    ctx.drawImage(el, 0, 0);",
  "    // Download.",
  "    const a = document.createElement('a');",
  "    document.body.append(a);",
  "    a.download = 'radarchart.png';",
  "    a.href = cloneCanvas.toDataURL('image/png');",
  "    a.click();",
  "    a.remove();",
  "  });",
  "}"
)

ui <- pageWithSidebar(
  headerPanel('Radarchart Shiny Example'),
  sidebarPanel(
    checkboxGroupInput('selectedPeople', 'Who to include', 
                       names(radarchart::skills)[-1], selected="Rich"),
    actionButton('downloadPlot', 'Download Plot')
  ),
  mainPanel(
    chartJSRadarOutput("plot1", width = "450", height = "300"), width = 7
  )
)

server <- function(input, output) {
  output$plot1 <- renderChartJSRadar({
    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE) %>% 
      onRender(js)
  })
}

shinyApp(ui, server)

这个出口到 png 仅限。使用 webshot 出口到 pdf.


编辑

library(shiny)
library(radarchart)

js <- paste0(c(
  "$(document).ready(function(){",
  "  $('#downloadPlot').on('click', function(){",
  "    var el = document.getElementById('plot1');",
  "    // Clone the chart to add a background color.",
  "    var cloneCanvas = document.createElement('canvas');",
  "    cloneCanvas.width = el.width;",
  "    cloneCanvas.height = el.height;",
  "    var ctx = cloneCanvas.getContext('2d');",
  "    ctx.fillStyle = '#FFFFFF';",
  "    ctx.fillRect(0, 0, el.width, el.height);",
  "    ctx.drawImage(el, 0, 0);",
  "    // Download.",
  "    const a = document.createElement('a');",
  "    document.body.append(a);",
  "    a.download = 'radarchart.png';",
  "    a.href = cloneCanvas.toDataURL('image/png');",
  "    a.click();",
  "    a.remove();",
  "    cloneCanvas.remove();",
  "  });",
  "});"
), collapse = "\n")

ui <- pageWithSidebar(
  headerPanel('Radarchart Shiny Example'),
  sidebarPanel(
    checkboxGroupInput('selectedPeople', 'Who to include', 
                       names(radarchart::skills)[-1], selected="Rich"),
    actionButton('downloadPlot', 'Download Plot')
  ),
  mainPanel(
    tags$head(tags$script(HTML(js))),
    chartJSRadarOutput("plot1", width = "450", height = "300"), width = 7
  )
)

server <- function(input, output) {
  output$plot1 <- renderChartJSRadar({
    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE) 
  })
}

shinyApp(ui, server)

1
投票

chartJSRadar 返回一个 htmlWidget. 要保存,请尝试使用 saveWidget 然后 webshot 暂时的 html 文件。添加 webshot 库。

library(webshot)

并尝试用这个来代替 downloadHandler 在你 server 职能。

output$downloadPlot <- downloadHandler(
  filename = "Shinyplot.png",
  content = function(file) {
    saveWidget(plot2(), "temp.html", selfcontained = TRUE)
    webshot("temp.html", file = file)
  }
) 
© www.soinside.com 2019 - 2024. All rights reserved.