Skip to contents

[Experimental]

Usage

tar_download_file(url, file)

Arguments

url

A character vector of URLs to download.

file

A character vector of the same length as url, with the paths to download to, relative to the user/ directory of the store.

Value

A character vector of the paths of the downloaded files, for a target with format = "file".

Details

Downloads source files into the user/ directory of the store of the pipeline that is running, for use in the command of a target with format = "file".

tar_make_archive() runs a pipeline with the working directory set to the pipeline's directory inside the installed package, while its store lives in the user cache directory. A relative path such as "_targets/user/data.xlsx" in a target's command therefore points into the package library, where the files are lost when the package is reinstalled, can't be written in a read-only library, and aren't removed by tar_destroy_archive(). tar_download_file() resolves file against targets::tar_path_store() instead, so downloaded files are kept with the store.

Each file is downloaded to a temporary file next to its destination and only moved into place once the download has succeeded, so a failed download never replaces a good file. A download fails with an error of class tarchives_error_download when the server responds with an HTTP error status, or returns an HTML page for a file whose extension isn't .html or .htm (typically an error page served after the file has moved).

The remote file isn't checked for changes on later builds. A changed url already invalidates the target, since it is part of the command. To download a file that was republished at the same URL, combine tar_download_file() with a target factory such as tarchetypes::tar_change().

Examples

if (identical(Sys.getenv("TAR_LONG_EXAMPLES"), "true")) {
  targets::tar_dir({
    targets::tar_script({
      list(
        targets::tar_target(
          file_manual,
          tarchives::tar_download_file(
            url = "https://cran.r-project.org/web/packages/tarchives/tarchives.pdf",
            file = "tarchives.pdf"
          ),
          format = "file"
        )
      )
    })
    targets::tar_make()
    targets::tar_read(file_manual)
  })
}