Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Post: screengrabs of shiny apps, automated #103

Open
gadenbuie opened this issue Feb 9, 2022 · 0 comments
Open

Post: screengrabs of shiny apps, automated #103

gadenbuie opened this issue Feb 9, 2022 · 0 comments

Comments

@gadenbuie
Copy link
Owner

library(shiny)
options(shiny.autoreload = FALSE)

app <- new.env()
source(system.file("examples", "01_hello", "app.R", package = "shiny"), local = app)

ui <- tagList(
  app$ui,
  tags$script(type = "module", HTML("
import { toPng } from 'https://cdn.skypack.dev/html-to-image';

const page = document.querySelector('body .container-fluid');

function screenGrabApp (shinyEventName) {
  toPng(page)
    .then(dataURL => Shiny.setInputValue('screenshot', {event: shinyEventName, data: dataURL}))
    .catch(console.error)
}

['value', 'idle', 'outputinvalidated'].forEach(function(name) {
  $(document).on('shiny:' + name, function() { screenGrabApp(name) })
})
"))
)

server <- function(input, output) {
  app$server(input, output)

  observeEvent(input$screenshot, {
    event <- input$screenshot$event
    time <- sub("[.]", "", strftime(Sys.time(), "screenshots/%F_%H%M%OS3"))
    path <- sprintf("%s_%s.png", time, event)
    message(path)
    data <- sub("data:image/png;base64,", "", input$screenshot$data, fixed = TRUE)
    data <- base64enc::base64decode(data)
    writeBin(data, path)
  })
}

dir.create("screenshots", showWarnings = FALSE)
shinyApp(ui, server, options = list(port = 6543))
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant