Skip to content

Commit

Permalink
loading and status
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 18, 2024
1 parent 0cc9915 commit 7f43a86
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: capture
Title: Capture Screenshot in Web Page
Version: 0.1.4.9000
Version: 0.1.5
Authors@R:
c(person(given = "Victor",
family = "Perrier",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Allow floating numbers in `scale =` argument [#13](https://github.com/dreamRs/capture/issues/13).
* Allow to pass JavaScript function in `options$filter` argument [#12](https://github.com/dreamRs/capture/issues/12).
* Added `loading` parameters to `capture()` like in `capture_pdf()`
* Added `statusInputId` parameter to `capture()` and `capture_pdf()` to retrieve status information in an `input` value server-side.


# capture 0.1.4
Expand Down
26 changes: 20 additions & 6 deletions R/capture.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#' @param inputId An `inputId` to retrieve image as base64 in an `input` slot server-side.
#' @param options Options (as a list) passed to [html-to-image](https://github.com/bubkoo/html-to-image#options)
#' method, for example you can use `backgroundColor` to set background color.
#' @param loading Add a loading indicator if taking screenshot take time, see [loading()] for usage.
#' @param statusInputId Retrieve status information in an `input` value server-side.
#' @param button_class Class to use for the HTML tag `<button>`
#'
#' @note It's only possible to take screenshot of elements that are actually visible on screen. It doesn't work in Internet Explorer.
Expand All @@ -30,6 +32,8 @@ capture <- function(selector,
scale = NULL,
inputId = NULL,
options = NULL,
loading = NULL,
statusInputId = NULL,
button_class = "btn btn-default") {
format <- match.arg(format)
ext <- tools::file_ext(filename)
Expand All @@ -43,7 +47,16 @@ capture <- function(selector,
`data-options` = toJSON(x = options, auto_unbox = TRUE),
`data-scale` = scale,
`data-inputId` = inputId,
...
`data-loading` = tolower(!is.null(loading)),
`data-status-id` = statusInputId,
...,
if (length(loading) > 0) {
tags$script(
type = "application/json",
`data-for` = "capture-loading-config",
toJSON(loading, auto_unbox = TRUE)
)
}
),
html_dependency_capture()
)
Expand All @@ -58,7 +71,6 @@ capture <- function(selector,
#'
#' @inheritParams capture
#' @param margins Margins to add to PDF.
#' @param loading Add a loading indicator if taking screenshot take time, see [loading()] for usage.
#'
#' @return an HTML tag that can be used in UI or rmarkdown HTML document.
#' @export
Expand All @@ -72,9 +84,10 @@ capture_pdf <- function(selector,
filename,
...,
margins = 15,
loading = NULL,
scale = NULL,
options = NULL,
loading = NULL,
statusInputId = NULL,
button_class = "btn btn-default") {
ext <- tools::file_ext(filename)
if (!identical(ext, "pdf"))
Expand All @@ -85,11 +98,12 @@ capture_pdf <- function(selector,
`data-selector` = selector,
`data-filename` = filename,
`data-margins` = margins,
`data-loading` = tolower(!is.null(loading)),
`data-options` = toJSON(x = options, auto_unbox = TRUE),
`data-scale` = if (!is.null(scale)) scale,
`data-scale` = scale,
`data-loading` = tolower(!is.null(loading)),
`data-status-id` = statusInputId,
...,
if (!is.null(loading)) {
if (length(loading) > 0) {
tags$script(
type = "application/json",
`data-for` = "capture-loading-config",
Expand Down
8 changes: 4 additions & 4 deletions R/loading.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

#' Loading indicator to be displayed when generating screenshot
#'
#'
#' @param text Text to be displayed below loading animation.
#' @param type Type of loading animation.
#' @param color Color for text and loading indicator.
#' @param background Background color.
#' @param size Size (in pixels).
#' @param ... Other arguments.
#'
#' @return a `list` that can be used in [capture_pdf()].
#' @return a `list` that can be used in [capture()] or [capture_pdf()].
#' @export
#'
loading <- function(text = "Generating PDF, please wait...",
Expand All @@ -18,7 +18,7 @@ loading <- function(text = "Generating PDF, please wait...",
size = "80px",
...) {
type <- match.arg(type)
list(
dropNulls(list(
text = text,
type = type,
options = list(
Expand All @@ -27,6 +27,6 @@ loading <- function(text = "Generating PDF, please wait...",
svgColor = color,
...
)
)
))
}

4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

dropNulls <- function(x) {
x[!vapply(x, is.null, FUN.VALUE = logical(1))]
}
3 changes: 1 addition & 2 deletions examples/default.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ ui <- fluidPage(
selector = "body",
filename = "all-page",
icon("camera"), "Take screenshot of all page",
format = "jpeg"
format = "png"
),
tags$br(),
fluidRow(
column(
width = 4,
wellPanel(
tags$b("Parameters :"),

selectInput(
inputId = "loi",
label = "Law:",
Expand Down
1 change: 0 additions & 1 deletion examples/scales-pdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ ui <- fluidPage(
width = 4,
wellPanel(
tags$b("Parameters :"),

selectInput(
inputId = "loi",
label = "Law:",
Expand Down
72 changes: 72 additions & 0 deletions examples/status-pdf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
library(shiny)
library(capture)

ui <- fluidPage(
tags$h2("Capture PDF (loading) example"),
capture_pdf(
selector = "body",
filename = "all-page",
icon("camera"), "Take screenshot of all page",
scale = 2,
statusInputId = "loading"
),
tags$br(),
fluidRow(
column(
width = 4,
wellPanel(
tags$b("Parameters :"),
selectInput(
inputId = "loi",
label = "Law:",
choices = c("normal", "uniform", "exponential")
)
)
),
column(
width = 8,
tags$div(
id = "result-block",
tags$b("Results :"),
plotOutput(outputId = "plot"),
uiOutput(outputId = "mean"),
verbatimTextOutput(outputId = "raw")
)
)
)
)

server <- function(input, output, session) {
distrib_r <- reactive({
switch(
input$loi,
"normal" = rnorm(1000),
"uniform" = runif(1000),
"exponential" = rexp(1000)
)
})

output$plot <- renderPlot({
hist(distrib_r())
})

output$mean <- renderUI({
tags$p(tags$b("The mean is :"), round(mean(distrib_r()), 2))
})

output$raw <- renderPrint({
summary(distrib_r())
})

observeEvent(input$loading, {
if (identical(input$loading$status, "started")) {
showNotification(ui = "Capturing screenshot, please wait...")
}
if (identical(input$loading$status, "finished")) {
showNotification(ui = "Screenshot captured!", type = "message")
}
})
}

if (interactive())
shinyApp(ui, server)
79 changes: 79 additions & 0 deletions examples/status.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
library(shiny)
library(capture)

ui <- fluidPage(
tags$h2("Capture (loading) example"),
capture(
selector = "body",
filename = "all-page",
icon("camera"), "Auto loading indicator",
loading = loading(text = "Capturing screenshot...")
),
capture(
selector = "body",
filename = "all-page",
icon("camera"), "Input value",
statusInputId = "loading"
),
tags$br(),
fluidRow(
column(
width = 4,
wellPanel(
tags$b("Parameters :"),
selectInput(
inputId = "loi",
label = "Law:",
choices = c("normal", "uniform", "exponential")
)
)
),
column(
width = 8,
tags$div(
id = "result-block",
tags$b("Results :"),
plotOutput(outputId = "plot"),
uiOutput(outputId = "mean"),
verbatimTextOutput(outputId = "raw")
)
)
)
)

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

distrib_r <- reactive({
switch(
input$loi,
"normal" = rnorm(1000),
"uniform" = runif(1000),
"exponential" = rexp(1000)
)
})

output$plot <- renderPlot({
hist(distrib_r())
})

output$mean <- renderUI({
tags$p(tags$b("The mean is :"), round(mean(distrib_r()), 2))
})

output$raw <- renderPrint({
summary(distrib_r())
})

observeEvent(input$loading, {
if (identical(input$loading$status, "started")) {
showNotification(ui = "Capturing screenshot, please wait...")
}
if (identical(input$loading$status, "finished")) {
showNotification(ui = "Screenshot captured!", type = "message")
}
})

}

if (interactive())
shinyApp(ui, server)
2 changes: 1 addition & 1 deletion inst/packer/capture-image.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/capture-image.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/capture-pdf.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/capture-pdf.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions man/capture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/capture_pdf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/loading.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7f43a86

Please sign in to comment.