Skip to content

Commit

Permalink
add httr_progress to use with the httr package #103
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Sep 2, 2021
1 parent 2161966 commit 2cb8e52
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: waiter
Title: Loading Screen for 'Shiny'
Version: 0.2.3
Version: 0.2.4.9000
Date: 2021-07-21
Authors@R: c(
person(given = "John",
Expand Down Expand Up @@ -28,6 +28,7 @@ Imports:
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Suggests:
httr,
knitr,
packer,
rmarkdown
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# waiter 0.2.4.9000

- Added `httr_progress` function to use instead of
`httr::progress`.

# waiter 0.2.3

- Fix [#95](https://github.com/JohnCoene/waiter/issues/95) with different CSS for full screen (`position: fixed`).
Expand Down
79 changes: 79 additions & 0 deletions R/httr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Waitress with httr
#'
#' Use a waitress progress bar with httr requests.
#' Simply use `httr_progress` where you would use
#' [httr::progress].
#'
#' @param waitress The waitress object.
#' @param type Type of progress to display: either number of
#' bytes uploaded or downloaded. Passed to [httr::progress].
#' @param pre,post Pre and callback functions to run before
#' the progress starts or once it is done.
#'
#' @examples
#' \dontrun{
#' cap_speed <- httr::config(max_recv_speed_large = 10000)
#'
#' httr::GET(
#' "http://httpbin.org/bytes/102400",
#' httr_progress(w),
#' cap_speed
#' )
#' }
httr_progress <- function(
waitress,
type = c("down", "up"),
pre = NULL,
post = NULL
){
type <- match.arg(type)

if(missing(waitress))
stop("Missing `waitress` object", call. = FALSE)

httr_request <- utils::getFromNamespace("request", "httr")

httr_request(
options = list(
noprogress = FALSE,
progressfunction = progress_bar(waitress, type, pre, post)
)
)
}

progress_bar <- function(w, type, pre = NULL, post = NULL) {

show_progress <- function(down, up) {
if (type == "down") {
total <- down[[1]]
now <- down[[2]]
} else {
total <- up[[1]]
now <- up[[2]]
}

if (total == 0 && now == 0) {
if(!is.null(pre))
pre()
# Reset progress bar when seeing first byte
w$start()
w$set(0)
} else {
w$max <- total

# increment
w$set(now)
# close
if(now == total) {
w$close()

if(!is.null(post))
post()
}
}

TRUE
}

show_progress
}
9 changes: 9 additions & 0 deletions R/waitress.R
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ Waitress <- R6::R6Class(
cat("A waitress notification\n")
}
),
active = list(
#' @field max Maximum value of the bar.
max = function(value) {
if(missing(value))
return(private$.max)

private$.max <- value
}
),
private = list(
.name = NULL,
.theme = NULL,
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

List of changes made to waiter.

# waiter 0.2.4.9000

- Added `httr_progress` function to use instead of
`httr::progress`.

## waiter 0.2.3

- Fix [#95](https://github.com/JohnCoene/waiter/issues/95) with different CSS for full screen (`position: fixed`).
Expand Down
42 changes: 41 additions & 1 deletion docs/waitress.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,44 @@ server <- function(input, output){
shinyApp(ui, server)
```

![](_assets/img/waitress-hide-on-render.gif)
![](_assets/img/waitress-hide-on-render.gif)

## httr

Use instead of `httr::progress`, replace it with `httr_progress`.

```r
library(httr)
library(shiny)
library(waiter)

cap_speed <- config(max_recv_speed_large = 10000)

ui <- fluidPage(
useWaitress(),
actionButton(
"dl",
"Download"
),
plotOutput("plot")
)

server <- function(input, output){
w <- Waitress$new("#plot")

dataset <- eventReactive(input$dl, {
x <- GET(
"http://httpbin.org/bytes/102400",
httr_progress(w),
cap_speed
)

runif(100)
})

output$plot <- renderPlot(plot(dataset()))
}

shinyApp(ui, server)

```
33 changes: 33 additions & 0 deletions man/httr_progress.Rd

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

7 changes: 7 additions & 0 deletions man/waitressClass.Rd

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

0 comments on commit 2cb8e52

Please sign in to comment.