将rpivottable输出导出为图像

将rpivottable输出导出为图像,r,shiny,rpivottable,R,Shiny,Rpivottable,我最近开始使用rPivotTable制作一些令人印象深刻的图表。我正在一个闪亮的应用程序中使用rPivotTable。我想知道是否可以将rPivotTable(表格、条形图、折线图等)的输出作为图像从web浏览器导出。在RStudio中(无光泽),可以执行此操作,因为查看器具有导出->另存为图像的选项。有没有办法保存图表和表格。数据透视表是htmlwidget,因此您可以使用htmlwidget::saveWidget将表格保存在html文件中,并webshot::webshot将其导出到png

我最近开始使用rPivotTable制作一些令人印象深刻的图表。我正在一个闪亮的应用程序中使用rPivotTable。我想知道是否可以将rPivotTable(表格、条形图、折线图等)的输出作为图像从web浏览器导出。在RStudio中(无光泽),可以执行此操作,因为查看器具有导出->另存为图像的选项。有没有办法保存图表和表格。

数据透视表是htmlwidget,因此您可以使用
htmlwidget::saveWidget
将表格保存在html文件中,并
webshot::webshot
将其导出到
png
(或
pdf

库(闪亮)
图书馆(rpivotTable)
库(htmlwidgets)
图书馆(网上热)

未找到在PhantomJ上侦听的ui。您可以使用webshot::install_phantomjs()安装它。如果安装了phantomjs,请确保可以通过PATH变量找到phantomjs可执行文件。@SuneelKumar那么您是否运行了
webshot::install_phantomjs()
?请在此处查看我的代码1。单击浏览按钮选择xlsx格式文件。2.在(选择一个…)中选择输入选择表格,然后单击“放手”按钮。然后点击下载数据。。。按钮数据将被下载,如果我通过点击下载窗口小部件选择图像,则只有绘图应被下载,而不是整个HTML窗口小部件
library(shiny)
library(rpivotTable)
library(htmlwidgets)
library(webshot)

ui <- fluidPage(
  br(),
  rpivotTableOutput("pivotbl"),
  br(),
  downloadButton("export", "Export")
)

server <- function(input, output, session){

  pivotTable <- rpivotTable(
    Titanic,
    rows = "Survived",
    cols = c("Class","Sex"),
    aggregatorName = "Sum as Fraction of Columns",
    inclusions = list( Survived = list("Yes")),
    exclusions= list( Class = list( "Crew")),
    vals = "Freq",
    rendererName = "Table Barchart"
  )

  output[["pivotbl"]] <- renderRpivotTable({
    pivotTable
  })

  output[["export"]] <- downloadHandler(
    filename = function(){
      "pivotTable.png"
    },
    content = function(file){
      tmphtml <- tempfile(fileext = ".html")
      saveWidget(pivotTable, file = tmphtml)
      webshot(tmphtml, file = file)
    }
  )

}

shinyApp(ui, server)
library(shiny)
library(rpivotTable)

js <- "
function filter(node){
  return (node.tagName !== 'i');
}
function exportPlot(filename){
  var plot = document.getElementsByClassName('pvtRendererArea');
  domtoimage.toPng(plot[0], {filter: filter, bgcolor: 'white'})
    .then(function (dataUrl) {
      var link = document.createElement('a');
      link.download = filename;
      link.href = dataUrl;
      link.click();
    });
}
Shiny.addCustomMessageHandler('export', exportPlot);
"

ui <- fluidPage(
  tags$head(
    tags$script(src = "dom-to-image.min.js"),
    tags$script(HTML(js))
  ),
  br(),
  rpivotTableOutput("pivotbl"),
  br(),
  actionButton("export", "Export")
)

server <- function(input, output, session){

  pivotTable <- rpivotTable(
    Titanic,
    rows = "Survived",
    cols = c("Class","Sex"),
    aggregatorName = "Sum as Fraction of Columns",
    inclusions = list( Survived = list("Yes")),
    exclusions= list( Class = list( "Crew")),
    vals = "Freq",
    rendererName = "Table Barchart"
  )

  output[["pivotbl"]] <- renderRpivotTable({
    pivotTable
  })

  observeEvent(input[["export"]], {
    session$sendCustomMessage("export", "plot.png")
  })  

}

shinyApp(ui, server)