是否可以一次单击将整个闪亮的应用程序(包括反应值和多个表格和图形)下载到 pdf 或图像?

Is it possible to download entire shiny application, including reactive values and multiple tables and graphs, to a pdf or image in one click?

很长一段时间以来,我一直在努力寻找解决这个问题的办法。我尝试了 appshot,但我的表格和图表显示为空白。

如有任何帮助或建议,我们将不胜感激。

您可以使用 html2canvas JavaScript 库来截屏。

为了创建下载提示,我参考了这个 post:

并且,要 运行 任意 JavaScript,您需要在 R 中加载 shinyjs 库。

请参阅下面的示例应用程序:

library(shiny)
library(shinyjs)
library(DT)
library(ggplot2)

ui <- fluidPage(
    tags$head(
        # include html2canvas library
        tags$script(src = "http://html2canvas.hertzen.com/dist/html2canvas.min.js"),
        # script for creating the prompt for download
        tags$script(
            "
            function saveAs(uri, filename) {

                var link = document.createElement('a');

                if (typeof link.download === 'string') {

                    link.href = uri;
                    link.download = filename;

                    //Firefox requires the link to be in the body
                    document.body.appendChild(link);

                    //simulate click
                    link.click();

                    //remove the link when done
                    document.body.removeChild(link);

                } else {

                    window.open(uri);

                }
            }
            "
        )
    ),
    useShinyjs(),
    actionButton("screenshot","Take Screenshot"),
    dataTableOutput("table"),
    plotOutput("plot")


)

server <- function(input, output, session) {
    observeEvent(input$screenshot,{
        shinyjs::runjs(
            'html2canvas(document.querySelector("body")).then(canvas => {
                saveAs(canvas.toDataURL(), "shinyapp.png");
           });'
        )
    })


    output$table <- renderDataTable(iris)

    output$plot <- renderPlot(ggplot(data = iris) + 
                                  geom_point(aes(x = Sepal.Length, y = Sepal.Width)))
}

shinyApp(ui, server)

您现在可以为此使用一个名为 shinyscreenshot 的专用包。它在引擎盖下使用 html2canvas 库,但使用起来非常简单 - 只需在您想拍照时调用 shinyscreenshot::screenshot(),它就会下载 PNG。