Skip to contents

download_file() downloads files from the internet to a local directory. It can handle multiple files at once and provides progress updates during the download process.

The function also checks for broken links and can return a list of any broken links encountered during the download.

Usage

download_file(url, dir = ".", broken_links = FALSE)

Arguments

url

A character vector of URLs pointing to files.

dir

(optional) A string specifying the directory where the files should be downloaded (default: ".").

(optional) A logical flag indicating whether to return a list of broken links (default: FALSE).

Value

If broken_links is TRUE, an invisible character vector of broken links. Otherwise, a invisible character vector of file paths where the files were downloaded.

See also

Other file functions: get_file_size()

Examples

library(curl)
#> Using libcurl 8.5.0 with OpenSSL/3.0.13

if (has_internet()) {
  urls <- paste0(
    "ftp://ftp.datasus.gov.br/dissemin/publicos/IBGE/POPSVS/",
     c("POPSBR00.zip", "POPSBR01.zip")
  )

  dir <- tempfile("dir")
  dir.create(dir)

  download_file(urls, dir)
}
#>  Downloading 2 files to /tmp/RtmptiZ4c3/dir237f1e31119f
#> 
#> Downloading data ■■■■■■■■■■■■■■■■                  50% | ETA:  5s
#> Downloading data ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% | ETA:  0s
#>