我已经包含了一个最小的例子。有一个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,还有许多其他工具可以让您按命令行截取屏幕截图,例如 ImageMagick或 scrot .
将 .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/