├── .gitignore ├── LICENSE ├── NAMESPACE ├── man ├── figures │ └── README-gif_progress_example.gif ├── runExample.Rd ├── progress_bar.Rd └── progress.Rd ├── .Rbuildignore ├── .travis.yml ├── CRAN-RELEASE ├── inst └── shiny-apps │ └── shinyhttrExample │ ├── server.R │ ├── ui.R │ └── modules │ └── pb.R ├── shinyhttr.Rproj ├── appveyor.yml ├── DESCRIPTION ├── LICENSE.md ├── README.md ├── README.Rmd └── R └── progress.R /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2019 2 | COPYRIGHT HOLDER: Athos Petri Damiani 3 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(progress) 4 | export(runExample) 5 | -------------------------------------------------------------------------------- /man/figures/README-gif_progress_example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/curso-r/shinyhttr/HEAD/man/figures/README-gif_progress_example.gif -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^CRAN-RELEASE$ 2 | ^appveyor\.yml$ 3 | ^\.travis\.yml$ 4 | ^README\.Rmd$ 5 | ^LICENSE\.md$ 6 | ^.*\.Rproj$ 7 | ^\.Rproj\.user$ 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r 2 | 3 | language: R 4 | sudo: false 5 | cache: packages 6 | -------------------------------------------------------------------------------- /CRAN-RELEASE: -------------------------------------------------------------------------------- 1 | This package was submitted to CRAN on 2019-03-19. 2 | Once it is accepted, delete this file and tag the release (commit 46fbd29844). 3 | -------------------------------------------------------------------------------- /inst/shiny-apps/shinyhttrExample/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(shinyWidgets) 3 | library(httr) 4 | library(shinyhttr) 5 | 6 | function(input, output, session) { 7 | shiny::callModule( 8 | module = pb, 9 | id = "pb_id" 10 | ) 11 | } 12 | -------------------------------------------------------------------------------- /inst/shiny-apps/shinyhttrExample/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(shinyWidgets) 3 | library(httr) 4 | library(shinyhttr) 5 | 6 | source("modules/pb.R") 7 | 8 | fluidPage( 9 | sidebarLayout( 10 | NULL, 11 | mainPanel( 12 | h3("shinyhttr example working with shiny modules."), 13 | pbUI(id = "pb_id") 14 | ) 15 | ) 16 | ) 17 | -------------------------------------------------------------------------------- /shinyhttr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: ISO8859-1 11 | 12 | RnwWeave: Sweave 13 | LaTeX: XeLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | -------------------------------------------------------------------------------- /man/runExample.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/progress.R 3 | \name{runExample} 4 | \alias{runExample} 5 | \title{runExample} 6 | \usage{ 7 | runExample(display.mode = "showcase") 8 | } 9 | \arguments{ 10 | \item{display.mode}{The mode in which to display the example. Defaults to showcase, but may be set to normal to see the example without code or commentary.} 11 | } 12 | \description{ 13 | Launch shiny example application using shinyhttr::progress_bar. This app also uses module to show that it works with it too. 14 | } 15 | -------------------------------------------------------------------------------- /inst/shiny-apps/shinyhttrExample/modules/pb.R: -------------------------------------------------------------------------------- 1 | # UI ----------------------------------# 2 | 3 | pbUI <- function(id) { 4 | 5 | ns <- shiny::NS(id) 6 | 7 | shiny::tagList( 8 | actionButton(ns('download'), 'Download 100MB file...'), 9 | tags$p('see R console to compare both progress bars.'), 10 | progressBar( 11 | id = ns('pb'), 12 | value = 0, 13 | title = '', 14 | display_pct = TRUE 15 | ) 16 | ) 17 | 18 | } 19 | 20 | # Server ----------------------------------# 21 | 22 | pb <- function(input, output, session) { 23 | observeEvent(input$download, { 24 | GET( 25 | url = 'https://speed.hetzner.de/100MB.bin', 26 | shinyhttr::progress(session, id = 'pb') 27 | ) 28 | }) 29 | } -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # DO NOT CHANGE the "init" and "install" sections below 2 | 3 | # Download script file from GitHub 4 | init: 5 | ps: | 6 | $ErrorActionPreference = "Stop" 7 | Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" 8 | Import-Module '..\appveyor-tool.ps1' 9 | 10 | install: 11 | ps: Bootstrap 12 | 13 | cache: 14 | - C:\RLibrary 15 | 16 | # Adapt as necessary starting from here 17 | 18 | build_script: 19 | - travis-tool.sh install_deps 20 | 21 | test_script: 22 | - travis-tool.sh run_tests 23 | 24 | on_failure: 25 | - 7z a failure.zip *.Rcheck\* 26 | - appveyor PushArtifact failure.zip 27 | 28 | artifacts: 29 | - path: '*.Rcheck\**\*.log' 30 | name: Logs 31 | 32 | - path: '*.Rcheck\**\*.out' 33 | name: Logs 34 | 35 | - path: '*.Rcheck\**\*.fail' 36 | name: Logs 37 | 38 | - path: '*.Rcheck\**\*.Rout' 39 | name: Logs 40 | 41 | - path: '\*_*.tar.gz' 42 | name: Bits 43 | 44 | - path: '\*_*.zip' 45 | name: Bits 46 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: shinyhttr 2 | Type: Package 3 | Title: Progress Bars for Downloads in 'shiny' Apps 4 | Version: 1.1.0 5 | Authors@R: 6 | c(person(given = "Athos", 7 | family = "Damiani", 8 | role = c("aut", "cre"), 9 | email = "athos.damiani@curso-r.com"), 10 | person(given = "Hadley", 11 | family = "Wickham", 12 | role = c("aut"), 13 | email = "hadley@rstudio.com"), 14 | person(given = "RStudio", 15 | role = "cph")) 16 | Description: Modifies the progress() function from 'httr' package to let it 17 | send output to progressBar() function from 'shinyWidgets' package. 18 | It is just a tweak at the original functions from 'httr' package to 19 | make it smooth for 'shiny' developers. 20 | License: MIT + file LICENSE 21 | BugReports: https://github.com/curso-r/shinyhttr/issues 22 | URL: https://github.com/curso-r/shinyhttr 23 | Encoding: UTF-8 24 | LazyData: true 25 | Imports: 26 | shinyWidgets, 27 | utils, 28 | httr 29 | RoxygenNote: 7.1.1 30 | Enhances: 31 | shiny 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Athos Petri Damiani 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /man/progress_bar.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/progress.R 3 | \name{progress_bar} 4 | \alias{progress_bar} 5 | \title{progress_bar} 6 | \usage{ 7 | progress_bar( 8 | type, 9 | con, 10 | session, 11 | id, 12 | title = NULL, 13 | status = NULL, 14 | range_value = NULL, 15 | unit_mark = "\%" 16 | ) 17 | } 18 | \arguments{ 19 | \item{type}{(from `httr::progress()` doc) Type of progress to display: either number of bytes uploaded or downloaded.} 20 | 21 | \item{con}{(from `httr::progress()` doc) Connection to send output too. Usually \code{stdout()} or \code{stderr}.} 22 | 23 | \item{session}{(from `shinyWidgets::updateProgressBar()` doc) The 'session' object passed to function given to shinyServer.} 24 | 25 | \item{id}{(from `shinyWidgets::updateProgressBar()` doc) An id used to update the progress bar.} 26 | 27 | \item{title}{(from `shinyWidgets::updateProgressBar()` doc) character, optional title.} 28 | 29 | \item{status}{(from `shinyWidgets::updateProgressBar()` doc) Color, must be a valid Bootstrap status : primary, info, success, warning, danger.} 30 | 31 | \item{range_value}{(from `shinyWidgets::updateProgressBar()` doc) Default is to display percentage ([0, 100]), but you can specify a custom range, e.g. -50, 50.} 32 | 33 | \item{unit_mark}{(from `shinyWidgets::updateProgressBar()` doc) Unit for value displayed on the progress bar, default to "\%".} 34 | } 35 | \value{ 36 | a function with rules to print out the progress. 37 | } 38 | \description{ 39 | Same as `httr:::progress_bar()` but with capability to talk to `shinyWidgets::progressBar()`. 40 | } 41 | \seealso{ 42 | \code{\link[shinyhttr]{progress}}, \code{\link[shinyWidgets:progressBar]{progressBar}} 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # shinyhttr 5 | 6 | [![Travis build 7 | status](https://travis-ci.org/curso-r/shinyhttr.svg?branch=master)](https://travis-ci.org/curso-r/shinyhttr) 8 | [![AppVeyor build 9 | status](https://ci.appveyor.com/api/projects/status/github/curso-r/shinyhttr?branch=master&svg=true)](https://ci.appveyor.com/project/curso-r/shinyhttr) 10 | [![CRAN 11 | status](https://www.r-pkg.org/badges/version/shinyhttr)](https://cran.r-project.org/package=shinyhttr) [![CRAN downloads](https://cranlogs.r-pkg.org/badges/shinyhttr)](https://cran.r-project.org/package=shinyhttr) 12 | 13 | The goal of shinyhttr is to integrate `httr::progress` with 14 | `shinyWidgets::progressBar`. 15 | 16 | In practice, the difference will be 17 | 18 | ``` r 19 | # from this 20 | httr::GET("http://download.com/large_file.txt", 21 | progress()) 22 | 23 | 24 | # to this 25 | httr::GET("http://download.com/large_file.txt", 26 | progress(session, id = "my_progress_bar1")) 27 | ``` 28 | 29 | ![gif\_progress\_example.gif](man/figures/README-gif_progress_example.gif) 30 | 31 | ## Installation 32 | 33 | From CRAN: 34 | 35 | ``` r 36 | install.packages("shinyhttr") 37 | ``` 38 | 39 | From github: 40 | 41 | ``` r 42 | devtools::install_github("curso-r/shinyhttr") 43 | ``` 44 | 45 | ## Example 46 | 47 | ``` r 48 | library(shiny) 49 | library(shinyWidgets) 50 | library(httr) 51 | library(shinyhttr) 52 | 53 | ui <- fluidPage( 54 | 55 | sidebarLayout( 56 | 57 | NULL, 58 | 59 | mainPanel( 60 | actionButton('download', 'Download 100MB file...'), 61 | tags$p("see R console to compare both progress bars."), 62 | progressBar( 63 | id = "pb", 64 | value = 0, 65 | title = "", 66 | display_pct = TRUE 67 | ) 68 | ) 69 | ) 70 | ) 71 | 72 | server <- function(input, output, session) { 73 | observeEvent(input$download, { 74 | GET( 75 | url = "https://speed.hetzner.de/100MB.bin", 76 | shinyhttr::progress(session, id = "pb") # <- the magic happens here. progress() now has session and id args 77 | ) 78 | }) 79 | } 80 | 81 | shinyApp(ui, server) 82 | ``` 83 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r setup, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures/README-", 12 | out.width = "100%" 13 | ) 14 | ``` 15 | 16 | # shinyhttr 17 | 18 | [![Travis build status](https://travis-ci.org/curso-r/shinyhttr.svg?branch=master)](https://travis-ci.org/curso-r/shinyhttr) [![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/curso-r/shinyhttr?branch=master&svg=true)](https://ci.appveyor.com/project/curso-r/shinyhttr) [![CRAN status](https://www.r-pkg.org/badges/version/shinyhttr)](https://cran.r-project.org/package=shinyhttr) [![CRAN downloads](https://cranlogs.r-pkg.org/badges/shinyhttr)](https://cran.r-project.org/package=shinyhttr) 19 | 20 | The goal of shinyhttr is to integrate `httr::progress` with `shinyWidgets::progressBar`. 21 | 22 | In practice, the difference will be 23 | 24 | ```{r, eval=FALSE} 25 | # from this 26 | httr::GET("http://download.com/large_file.txt", 27 | progress()) 28 | 29 | 30 | # to this 31 | httr::GET("http://download.com/large_file.txt", 32 | progress(session, id = "my_progress_bar1")) 33 | ``` 34 | 35 | 36 | ![gif_progress_example.gif](man/figures/README-gif_progress_example.gif) 37 | 38 | ## Installation 39 | 40 | From CRAN: 41 | 42 | ```{r, eval=FALSE} 43 | install.packages("shinyhttr") 44 | ``` 45 | 46 | From github: 47 | 48 | ```{r, eval=FALSE} 49 | devtools::install_github("curso-r/shinyhttr") 50 | ``` 51 | 52 | ## Example 53 | 54 | ```{r, eval=FALSE} 55 | library(shiny) 56 | library(shinyWidgets) 57 | library(httr) 58 | library(shinyhttr) 59 | 60 | ui <- fluidPage( 61 | 62 | sidebarLayout( 63 | 64 | NULL, 65 | 66 | mainPanel( 67 | actionButton('download', 'Download 100MB file...'), 68 | tags$p("see R console to compare both progress bars."), 69 | progressBar( 70 | id = "pb", 71 | value = 0, 72 | title = "", 73 | display_pct = TRUE 74 | ) 75 | ) 76 | ) 77 | ) 78 | 79 | server <- function(input, output, session) { 80 | observeEvent(input$download, { 81 | GET( 82 | url = "https://speed.hetzner.de/100MB.bin", 83 | shinyhttr::progress(session, id = "pb") # <- the magic happens here. progress() now has session and id args 84 | ) 85 | }) 86 | } 87 | 88 | shinyApp(ui, server) 89 | ``` 90 | 91 | -------------------------------------------------------------------------------- /man/progress.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/progress.R 3 | \name{progress} 4 | \alias{progress} 5 | \title{Add a progress bar compatible with `shinyWidgets::updateProgressBar()`.} 6 | \usage{ 7 | progress( 8 | session, 9 | id, 10 | type = c("down", "up"), 11 | con = stdout(), 12 | title = NULL, 13 | status = NULL, 14 | range_value = NULL, 15 | unit_mark = "\%" 16 | ) 17 | } 18 | \arguments{ 19 | \item{session}{(from `shinyWidgets::updateProgressBar()`` doc) The 'session' object passed to function given to shinyServer.} 20 | 21 | \item{id}{(from `shinyWidgets::updateProgressBar()` doc) An id used to update the progress bar.} 22 | 23 | \item{type}{(from `httr::progress()`` doc) Type of progress to display: either number of bytes uploaded or downloaded.} 24 | 25 | \item{con}{(from `httr::progress()` doc) Connection to send output too. Usually \code{stdout()} or \code{stderr}.} 26 | 27 | \item{title}{(from `shinyWidgets::updateProgressBar()` doc) character, optional title.} 28 | 29 | \item{status}{(from `shinyWidgets::updateProgressBar()` doc) Color, must be a valid Bootstrap status : primary, info, success, warning, danger.} 30 | 31 | \item{range_value}{(from `shinyWidgets::updateProgressBar()` doc) Default is to display percentage ([0, 100]), but you can specify a custom range, e.g. -50, 50.} 32 | 33 | \item{unit_mark}{(from `shinyWidgets::updateProgressBar()` doc) Unit for value displayed on the progress bar, default to "\%".} 34 | } 35 | \description{ 36 | Add a progress bar to request just like the vanilla `httr::progress()` but with capability to talk to `shinyWidgets::updateProgressBar()` by giving the session and id arguments to it. 37 | } 38 | \examples{ 39 | 40 | if (interactive()) { 41 | 42 | library(shiny) 43 | library(shinyWidgets) 44 | library(shinyhttr) 45 | 46 | ui <- fluidPage( 47 | 48 | sidebarLayout( 49 | 50 | NULL, 51 | 52 | mainPanel( 53 | actionButton('download', 'Download 100MB file...'), 54 | tags$p("see R console to compare both progress bars."), 55 | progressBar( 56 | id = "pb", 57 | value = 0, 58 | title = "", 59 | display_pct = TRUE 60 | ) 61 | ) 62 | ) 63 | ) 64 | 65 | server <- function(input, output, session) { 66 | observeEvent(input$download, { 67 | httr::GET( 68 | url = "https://speed.hetzner.de/100MB.bin", 69 | progress(session, id = "pb") 70 | ) 71 | }) 72 | } 73 | 74 | shinyApp(ui, server) 75 | } 76 | } 77 | \seealso{ 78 | \code{\link[httr:progress]{progress}}, \code{\link[shinyWidgets:progressBar]{progressBar}}, \code{\link[shinyWidgets:updateProgressBar]{updateProgressBar}} 79 | } 80 | -------------------------------------------------------------------------------- /R/progress.R: -------------------------------------------------------------------------------- 1 | #' Add a progress bar compatible with `shinyWidgets::updateProgressBar()`. 2 | #' 3 | #' Add a progress bar to request just like the vanilla `httr::progress()` but with capability to talk to `shinyWidgets::updateProgressBar()` by giving the session and id arguments to it. 4 | #' 5 | #' @param session (from `shinyWidgets::updateProgressBar()`` doc) The 'session' object passed to function given to shinyServer. 6 | #' @param id (from `shinyWidgets::updateProgressBar()` doc) An id used to update the progress bar. 7 | #' @param type (from `httr::progress()`` doc) Type of progress to display: either number of bytes uploaded or downloaded. 8 | #' @param con (from `httr::progress()` doc) Connection to send output too. Usually \code{stdout()} or \code{stderr}. 9 | #' @param title (from `shinyWidgets::updateProgressBar()` doc) character, optional title. 10 | #' @param status (from `shinyWidgets::updateProgressBar()` doc) Color, must be a valid Bootstrap status : primary, info, success, warning, danger. 11 | #' @param range_value (from `shinyWidgets::updateProgressBar()` doc) Default is to display percentage ([0, 100]), but you can specify a custom range, e.g. -50, 50. 12 | #' @param unit_mark (from `shinyWidgets::updateProgressBar()` doc) Unit for value displayed on the progress bar, default to "\%". 13 | #' 14 | #' @export 15 | #' 16 | #' 17 | #' @seealso \code{\link[httr:progress]{progress}}, \code{\link[shinyWidgets:progressBar]{progressBar}}, \code{\link[shinyWidgets:updateProgressBar]{updateProgressBar}} 18 | #' 19 | #' @examples 20 | #' 21 | #' if (interactive()) { 22 | #' 23 | #' library(shiny) 24 | #' library(shinyWidgets) 25 | #' library(shinyhttr) 26 | #' 27 | #' ui <- fluidPage( 28 | #' 29 | #' sidebarLayout( 30 | #' 31 | #' NULL, 32 | #' 33 | #' mainPanel( 34 | #' actionButton('download', 'Download 100MB file...'), 35 | #' tags$p("see R console to compare both progress bars."), 36 | #' progressBar( 37 | #' id = "pb", 38 | #' value = 0, 39 | #' title = "", 40 | #' display_pct = TRUE 41 | #' ) 42 | #' ) 43 | #' ) 44 | #' ) 45 | #' 46 | #' server <- function(input, output, session) { 47 | #' observeEvent(input$download, { 48 | #' httr::GET( 49 | #' url = "https://speed.hetzner.de/100MB.bin", 50 | #' progress(session, id = "pb") 51 | #' ) 52 | #' }) 53 | #' } 54 | #' 55 | #' shinyApp(ui, server) 56 | #' } 57 | progress <- function ( 58 | session, 59 | id, 60 | 61 | type = c("down", "up"), 62 | con = stdout(), 63 | 64 | title = NULL, 65 | status = NULL, 66 | range_value = NULL, 67 | unit_mark = "%" 68 | ) { 69 | request <- utils::getFromNamespace("request", "httr") 70 | type <- match.arg(type) 71 | request(options = list( 72 | noprogress = FALSE, 73 | progressfunction = progress_bar( 74 | type, 75 | con, 76 | 77 | session = session, 78 | id = id, 79 | title = title, 80 | status = status, 81 | range_value = range_value, 82 | unit_mark = unit_mark 83 | ) 84 | ) 85 | ) 86 | } 87 | 88 | 89 | #' progress_bar 90 | #' 91 | #' Same as `httr:::progress_bar()` but with capability to talk to `shinyWidgets::progressBar()`. 92 | #' 93 | #' @param type (from `httr::progress()` doc) Type of progress to display: either number of bytes uploaded or downloaded. 94 | #' @param con (from `httr::progress()` doc) Connection to send output too. Usually \code{stdout()} or \code{stderr}. 95 | #' @param session (from `shinyWidgets::updateProgressBar()` doc) The 'session' object passed to function given to shinyServer. 96 | #' @param id (from `shinyWidgets::updateProgressBar()` doc) An id used to update the progress bar. 97 | #' @param title (from `shinyWidgets::updateProgressBar()` doc) character, optional title. 98 | #' @param status (from `shinyWidgets::updateProgressBar()` doc) Color, must be a valid Bootstrap status : primary, info, success, warning, danger. 99 | #' @param range_value (from `shinyWidgets::updateProgressBar()` doc) Default is to display percentage ([0, 100]), but you can specify a custom range, e.g. -50, 50. 100 | #' @param unit_mark (from `shinyWidgets::updateProgressBar()` doc) Unit for value displayed on the progress bar, default to "\%". 101 | #' 102 | #' 103 | #' @return a function with rules to print out the progress. 104 | #' 105 | #' @seealso \code{\link[shinyhttr]{progress}}, \code{\link[shinyWidgets:progressBar]{progressBar}} 106 | #' 107 | progress_bar <- function ( 108 | type, 109 | con, 110 | 111 | session, 112 | id, 113 | title = NULL, 114 | status = NULL, 115 | range_value = NULL, 116 | unit_mark = "%" 117 | ) { 118 | bytes <- utils::getFromNamespace("bytes", "httr") 119 | bar <- NULL 120 | show_progress <- function(down, up) { 121 | if (type == "down") { 122 | total <- down[[1]] 123 | now <- down[[2]] 124 | } else { 125 | total <- up[[1]] 126 | now <- up[[2]] 127 | } 128 | if (total == 0 && now == 0) { 129 | bar <<- NULL 130 | } else if (total == 0) { 131 | cat("\rDownloading: ", bytes(now, digits = 2), " ", 132 | sep = "", file = con) 133 | utils::flush.console() 134 | } else { 135 | if (is.null(bar)) { 136 | bar <<- utils::txtProgressBar(max = total, style = 3, file = con) 137 | } 138 | utils::setTxtProgressBar(bar, now) 139 | if (now == total) 140 | close(bar) 141 | } 142 | shinyWidgets::updateProgressBar( 143 | session = session, 144 | id = session$ns(id), 145 | value = round(now/total * 100, 0), 146 | title = title, 147 | status = status, 148 | range_value = range_value, 149 | unit_mark = unit_mark 150 | ) 151 | 152 | TRUE 153 | } 154 | show_progress 155 | } 156 | 157 | 158 | #' runExample 159 | #' 160 | #' Launch shiny example application using shinyhttr::progress_bar. This app also uses module to show that it works with it too. 161 | #' 162 | #' @param display.mode The mode in which to display the example. Defaults to showcase, but may be set to normal to see the example without code or commentary. 163 | #' 164 | #' @export 165 | runExample <- function(display.mode = "showcase") { 166 | appDir <- system.file("shiny-apps", "shinyhttrExample", package = "shinyhttr") 167 | if (appDir == "") { 168 | stop("Could not find example directory. Try re-installing `shinyhttr`.", call. = FALSE) 169 | } 170 | 171 | shiny::runApp(appDir, display.mode = display.mode) 172 | } 173 | --------------------------------------------------------------------------------