r - 如何在单击按钮时截取 Shiny 应用程序的屏幕截图?

标签 r shiny

我已经包含了一个最小的例子。有一个download按钮,点击后应将 Shiny 的应用程序屏幕截图下载为 pdf。代码如下。

library(shiny)
server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })

}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      actionButton("btn", "Download")

    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)

提前致谢!

最佳答案

正如评论中提到的,我尝试使用 RSelenium包在 Shiny-App 中截取屏幕截图。但显然存在与 webshots 相同的问题. session 被阻塞,所以 phantomjs无法访问该网站。

我找到了一个适用于 Windows 的解决方案,但它需要 this批处理文件,它将截取整个屏幕的屏幕截图,而不仅仅是 Shiny 的应用程序。对于 Linux,还有许多其他工具可以让您按命令行截取屏幕截图,例如 ImageMagickscrot .

将 .bat 文件放在与你的 Shiny-app 相同的目录中,加载应用程序,点击下载,允许 windows/anti-virus 程序,它会截取你的窗口。

您还可以保存多张图片,尽管我会想出比我的更复杂的命名方法。 ;)

library(shiny)
library(RSelenium)

ui <- {fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      actionButton("btn", "Download")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)}

server <- function(input, output, session) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })

  observeEvent(input$btn, {
    img = paste0("screen", runif(1,0,1000), ".png")
    str = paste('call screenCapture ', img)
    shell(str)
  })
}

shinyApp(ui = ui, server = server)

为了移除浏览器和 Windows 工具栏,我对 .bat 文件进行了一些操作。

第 66 行:
int height = windowRect.bottom - windowRect.top - 37;

第 75 行:
GDI32.BitBlt(hdcDest, 0, -80, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);

这适用于我的机器,但您必须调整这些值,甚至提出更好的解决方案,因为我不得不承认我不太擅长批处理脚本。这将隐藏工具栏,但底部会有一个黑色 strip 。

这是RSelenium的实验, 其中 没用 .
library(shiny)
library(RSelenium)

ui <- {fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      actionButton("btn", "Download")

    ),
    mainPanel(plotOutput("distPlot"))
  )
)}

server <- function(input, output, session) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })

  observeEvent(input$btn, {
    cdat <- session$clientData
    url <- paste0(cdat$url_protocol,"//",cdat$url_hostname,":", cdat$url_port, cdat$url_pathname,cdat$url_search)
    rD <- rsDriver(browser = "firefox", chromever=NULL)
    remDr <- rD$client
    remDr$navigate(url)
    remDr$screenshot(file = tf <- tempfile(fileext = ".png"))
    shell.exec(tf) # on windows
    remDr$close()
    rD$server$stop()
  })
}

shinyApp(ui = ui, server = server)

关于r - 如何在单击按钮时截取 Shiny 应用程序的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45416331/

相关文章:

通过R中的字长以不同方式替换字符串中的标点符号

r - 迁移 R 库

r - 如何为特定选项卡隐藏 Shiny 应用程序的侧边栏面板

r - Shiny -为什么要更改 tabpanel 的顺序,tabPanel 不显示绘图?

r - 使用 ReporteRs 包通过 R Shiny 生成 powerpoint 幻灯片

RStudio Sweave 错误 -> 退出代码 -1073740791

r - 提取黄土局部多项式函数中的拟合项(在 R 中)(不是 Predict())

r - 如何更改R中所有用户的文件权限

R Shiny 设置复选框控制值到数据框特定列中的值

Shiny 的 R : How can I adjust radio buttons to the grid width of column?