| Title: | Download and Manage Optional Package Data |
|---|---|
| Description: | Manage optional data for your package. The data can be hosted anywhere, and you have to give a Uniform Resource Locator (URL) for each file. File integrity checks are supported. This is useful for package authors who need to ship more than the 5 Megabyte of data currently allowed by the the Comprehensive R Archive Network (CRAN). Download functions are supposed to be called by users in interactive sessions only. |
| Authors: | Tim Schäfer [aut, cre] (ORCID: <https://orcid.org/0000-0002-3683-8070>) |
| Maintainer: | Tim Schäfer <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.1 |
| Built: | 2026-09-30 12:43:43 UTC |
| Source: | https://github.com/dfsp-spirit/pkgfilecache |
Check whether the given files exist in the package cache. You can pass MD5 sums, which will be verified and only files with correct MD5 hash will count as existing.
are_files_available(pkg_info, relative_filenames, md5sums = NULL)are_files_available(pkg_info, relative_filenames, md5sums = NULL)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
relative_filenames |
vector of strings. A vector of filenames, relative to the package cache. |
md5sums |
vector of strings or NULL. A list of MD5 checksums, one for each file in param 'relative_filenames', if not NULL. If given, the files will only be reported as existing if the MD5 sums match. |
logical vector. For each file, whether it passed the check.
pkg_info = get_pkg_info("mypackage") is_available = are_files_available(pkg_info, c("file1.txt", "file2.txt"))pkg_info = get_pkg_info("mypackage") is_available = are_files_available(pkg_info, c("file1.txt", "file2.txt"))
Ensure all given files exist in the file cache, download them if they are not.
ensure_files_available( pkg_info, relative_filenames, urls, files_are_binary = NULL, md5sums = NULL, on_errors = "warn", download = TRUE, num_connections = getOption("pkgfilecache.num_connections", 2), num_retries = 2 )ensure_files_available( pkg_info, relative_filenames, urls, files_are_binary = NULL, md5sums = NULL, on_errors = "warn", download = TRUE, num_connections = getOption("pkgfilecache.num_connections", 2), num_retries = 2 )
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
relative_filenames |
vector of strings. A vector of filenames, realtive to the package cache. |
urls |
vector of strings. For each file, a remote URL where to download the file. Will be passed to 'curl::curl_download', see that function for URL encoding details. |
files_are_binary |
logical vector. For each file, whether it is binary. Only required on Windows, when files need to be downloaded. See 'curl::curl_download' docs for details. |
md5sums |
vector of strings or NULL. A list of MD5 checksums, one for each file in param 'relative_filenames', if not NULL. If given, the files will only be reported as existing if the MD5 sums match. A file that is present in the cache but whose MD5 sum does not match the expected sum is considered invalid and is deleted from the cache, so that it is never mistaken for a valid file. |
on_errors |
string. What to do if getting the files failed. One of c("warn", "stop", "ignore"). At the end, files are checked using ‘files_available'(including MD5 if given). Depending on the check results, the behaviours triggered are: "warn": Print a warning for each file that failed the check. "stop": Stop the script, i.e., the whole application. "ignore": Do nothing. You can still react using the return value. This applies whether or not downloading was attempted, so missing files are never silently ignored, e.g., when ’download' is FALSE. |
download |
logical. Whether to try downloading missing files. Defaults to TRUE. Existing files (with correct MD5 if available) will never be downloaded. When set to FALSE, no files are downloaded, but missing files are still reported according to 'on_errors'. |
num_connections |
integer. The number of parallel connections to use when downloading files. Defaults to 2. With more connections, several files are downloaded at the same time, which can speed up downloading many small files considerably. Use 1 for strictly sequential downloads. A high number of connections may overload the server or trigger rate limits. The default can be changed globally for all calls that do not specify this argument by setting the R option |
num_retries |
integer. How many times to retry downloading files that failed, in addition to the first attempt. Defaults to 2. Each attempt uses fresh connections, which makes downloads robust against transient connection failures (e.g., a server that closes or throttles connections). Set to 0 to disable retrying. |
Named list. The list has entries: "available": vector of strings. The names of the files that are available in the local file cache. You can access them using get_filepath(). "missing": vector of strings. The names of the files that this function was unable to retrieve. "file_status": Logical array indicating whether the files are available. Order is identical to the one in argument 'relative_filenames'.
pkg_info = get_pkg_info("mypackage"); local_relative_filenames = c("local_file1.txt", "local_file2.txt"); bu = "https://raw.githubusercontent.com/dfsp-spirit/"; url1 = paste(bu, "pkgfilecache/master/inst/extdata/file1.txt", sep=""); url2 = paste(bu, "pkgfilecache/master/inst/extdata/file2.txt", sep=""); urls = c(url1, url2); md5sums = c("35261471bcd198583c3805ee2a543b1f", "85ffec2e6efb476f1ee1e3e7fddd86de"); res = ensure_files_available(pkg_info, local_relative_filenames, urls, md5sums=md5sums); erase_file_cache(pkg_info); # clear full cachepkg_info = get_pkg_info("mypackage"); local_relative_filenames = c("local_file1.txt", "local_file2.txt"); bu = "https://raw.githubusercontent.com/dfsp-spirit/"; url1 = paste(bu, "pkgfilecache/master/inst/extdata/file1.txt", sep=""); url2 = paste(bu, "pkgfilecache/master/inst/extdata/file2.txt", sep=""); urls = c(url1, url2); md5sums = c("35261471bcd198583c3805ee2a543b1f", "85ffec2e6efb476f1ee1e3e7fddd86de"); res = ensure_files_available(pkg_info, local_relative_filenames, urls, md5sums=md5sums); erase_file_cache(pkg_info); # clear full cache
Like ensure_files_available, but the files are described in a single declarative manifest (one row per file) instead of three parallel vectors. A manifest can be a CSV file (e.g., shipped with the package in inst/extdata) or a data.frame, see read_manifest for the expected format.
ensure_files_available_from_manifest( pkg_info, manifest, base_url = NULL, files_are_binary = NULL, on_errors = "warn", download = TRUE, num_connections = getOption("pkgfilecache.num_connections", 2), num_retries = 2 )ensure_files_available_from_manifest( pkg_info, manifest, base_url = NULL, files_are_binary = NULL, on_errors = "warn", download = TRUE, num_connections = getOption("pkgfilecache.num_connections", 2), num_retries = 2 )
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
manifest |
string or data.frame. The manifest to use, see |
base_url |
string or NULL. Base URL to derive the download URL for files that have no explicit URL, see |
files_are_binary |
logical vector. For each file, whether it is binary. Only required on Windows, when files need to be downloaded. See |
on_errors |
string. What to do if getting the files failed. One of c("warn", "stop", "ignore"). See |
download |
logical. Whether to try downloading missing files. Defaults to TRUE. Existing files (with correct MD5 if available) will never be downloaded. Set to FALSE to only check which files are available without downloading anything. |
num_connections |
integer. The number of parallel connections to use when downloading files, see |
num_retries |
integer. How many times to retry downloading files that failed, in addition to the first attempt, see |
Named list, like for ensure_files_available: the entries "available" and "missing" contain the manifest paths (as '/'-separated strings) that are available in, or missing from, the local file cache. The entry "file_status" is a logical vector in manifest row order indicating for each file whether it is available.
pkg_info = get_pkg_info("mypackage") # A manifest as a data.frame: two files, URLs derived from base_url. manifest = data.frame(path = c("sub/file1.txt", "file2.txt"), stringsAsFactors = FALSE) # Only check availability, do not download anything (download = FALSE). # The files are missing, so a warning is expected (see 'on_errors'). res = suppressWarnings(ensure_files_available_from_manifest(pkg_info, manifest, base_url = "https://example.com/data/", download = FALSE))pkg_info = get_pkg_info("mypackage") # A manifest as a data.frame: two files, URLs derived from base_url. manifest = data.frame(path = c("sub/file1.txt", "file2.txt"), stringsAsFactors = FALSE) # Only check availability, do not download anything (download = FALSE). # The files are missing, so a warning is expected (see 'on_errors'). res = suppressWarnings(ensure_files_available_from_manifest(pkg_info, manifest, base_url = "https://example.com/data/", download = FALSE))
Delete the full package cache directory for the given package.
erase_file_cache(pkg_info)erase_file_cache(pkg_info)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
integer. The return value of the unlink() call: 0 for success, 1 for failure. See the unlink() documentation for details.
Construct absolute path for package cache files.
get_absolute_path_for_files(pkg_info, relative_filenames)get_absolute_path_for_files(pkg_info, relative_filenames)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
relative_filenames |
vector of strings. A vector of filenames, relative to the package cache. |
vector of strings. The absolute paths.
rel_files = c("file1.txt", "file2.txt") pkg_info = get_pkg_info("mypackage") abs_paths = get_absolute_path_for_files(pkg_info, rel_files)rel_files = c("file1.txt", "file2.txt") pkg_info = get_pkg_info("mypackage") abs_paths = get_absolute_path_for_files(pkg_info, rel_files)
Get the absolute path of the package cache directory for the given package.
By default, the cache is stored in the directory returned by 'tools::R_user_dir(packagename, "data")' (for R version 4.0 or later), which is the location recommended by the CRAN repository policy for user-specific data and cache files. If a cache already exists at the legacy location used by older versions of this package (the directory returned by 'rappdirs::user_data_dir'), that legacy directory is reused instead, so that existing downloads are not lost and do not have to be re-downloaded. On R versions before 4.0, the legacy 'rappdirs::user_data_dir' location is used.
The location can be overridden with R options:
'pkgfilecache.cachedir': If set to a directory path, that directory is used as the root of the package cache, and the package name (and version, if any) are appended to it. Useful to place the cache elsewhere, e.g., on a ramdisk or network drive.
‘pkgfilecache.use_tempdir': If set to 'TRUE', the cache is placed in a subdirectory of the R session’s temporary directory (‘tempdir()'). This is handy for unit tests and continuous integration, and in particular for tests that must not write to the user’s home directory (e.g., on CRAN). The value of 'pkgfilecache.cachedir' takes precedence if both options are set.
get_cache_dir(pkg_info)get_cache_dir(pkg_info)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
string. The absolute path of the package cache directory. A subdirectory of the directory returned by 'tools::R_user_dir' (for R version 4.0 or later) or 'rappdirs::user_data_dir' (for older R versions), unless one of the options 'pkgfilecache.cachedir' or 'pkgfilecache.use_tempdir' is set.
pkg_info = get_pkg_info("mypackage") opt_data_dir = get_cache_dir(pkg_info)pkg_info = get_pkg_info("mypackage") opt_data_dir = get_cache_dir(pkg_info)
Retrieve the path to a single file from the package cache.
get_filepath(pkg_info, relative_filename, mustWork = TRUE)get_filepath(pkg_info, relative_filename, mustWork = TRUE)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
relative_filename |
string. A filename, relative to the package cache. |
mustWork |
logical. Whether an error should be created if the file does not exist. |
string. The path to the file. If mustWork=TRUE, the file is guaranteed to exist if the function returns (an error will occur if it does not). If mustWork=FALSE and the file does not exist, the empty string is returned.
pkg_info = get_pkg_info("mypackage") full_path_of_file = get_filepath(pkg_info, "file1.txt", mustWork=FALSE)pkg_info = get_pkg_info("mypackage") full_path_of_file = get_filepath(pkg_info, "file1.txt", mustWork=FALSE)
This functions constructs an object that uniquely identifies your package, i.e., the package that want to use the package cache. This is not a secret.
get_pkg_info(packagename, author = NULL, version = NULL)get_pkg_info(packagename, author = NULL, version = NULL)
packagename |
string. The name of the package using the package cache. Must be a valid directory name. Should not contain spaces. Passed as 'appname' to 'rappdirs::user_data_dir'. |
author |
string. The author of the package using the package cache, or NULL. Must be a valid directory name if given, no need for the real author name. Should not contain spaces. Defaults to NULL. Passed as 'appauthor' to 'rappdirs::user_data_dir'. Leave at NULL if in doubt. |
version |
string or NULL. An optional version path element to append to the path. You might want to use this if you want multiple versions of your pacakge to be able to have independent data. If used, this would typically be "<major>.<minor>". Must be a valid directory name. Should not contain spaces or special characters. |
named list. This can be passed to all function which require a 'pkg_info' argument. You should not care for the inner structure and treat it as some identifier.
pkg_info = get_pkg_info("mypackage") pkg_info = get_pkg_info("mypackage", author="me") pkg_info = get_pkg_info("mypackage", author="me", version="0.3")pkg_info = get_pkg_info("mypackage") pkg_info = get_pkg_info("mypackage", author="me") pkg_info = get_pkg_info("mypackage", author="me", version="0.3")
List files that are available locally in the package cache.
list_available(pkg_info)list_available(pkg_info)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
vector of strings. The file names available, relative to the package cache. The returned names may include a subdirectory part. The subdirectories are not listed separately.
pkg_info = get_pkg_info("mypackage") available_files_in_cache = list_available(pkg_info)pkg_info = get_pkg_info("mypackage") available_files_in_cache = list_available(pkg_info)
Generate a manifest CSV from a directory of files from the command line. This function is meant to be run with Rscript, not to be called interactively. It reads the command line arguments, parses them, and calls write_manifest_from_dir. It is the engine behind the standalone make_manifest.R script that ships with the package (see manifest_script).
manifest_cli(args = commandArgs(trailingOnly = TRUE))manifest_cli(args = commandArgs(trailingOnly = TRUE))
args |
character vector. The command line arguments. Defaults to the arguments actually passed to the R script ( |
Supported arguments (each flag can be given as --flag value or as --flag=value):
--dir <path>: The directory containing the data files (required).
--out <path>: The path of the CSV file to write the manifest to (required).
--url-base <url>: Optional base URL used to derive the download URL of every file, as paste0(url_base, path).
--help: Print usage information and exit without generating anything.
A leading --args or -- marker (used by Rscript to separate command line arguments from R options) is ignored, so both the one-liner form and the script form shown in the examples work.
The generated manifest as a data.frame, invisibly, or NULL if --help was given.
## Not run: # One-liner from the shell (no script file needed): Rscript -e 'pkgfilecache::manifest_cli()' --args --dir ~/mydata --out files.csv # Same thing using the standalone script shipped with the package # (--url-base is optional and derives the download URLs): Rscript "$(Rscript -e 'cat(pkgfilecache::manifest_script())')" --dir ~/mydata --out files.csv ## End(Not run)## Not run: # One-liner from the shell (no script file needed): Rscript -e 'pkgfilecache::manifest_cli()' --args --dir ~/mydata --out files.csv # Same thing using the standalone script shipped with the package # (--url-base is optional and derives the download URLs): Rscript "$(Rscript -e 'cat(pkgfilecache::manifest_script())')" --dir ~/mydata --out files.csv ## End(Not run)
Return the full path to the make_manifest.R script that ships with the package (installed into the package's exec subdirectory). This script is a thin wrapper around manifest_cli and can be run with Rscript to generate a manifest from the command line. On Unix-like systems you can also copy it to a directory on your PATH and make it executable with chmod +x, then run it like a normal command.
manifest_script()manifest_script()
character string. The full path to the script, or "" if the script cannot be found (e.g., because the package is not installed correctly).
Read a manifest that describes a set of optional data files, one file per row. A manifest is either a CSV file or a data.frame with the columns described below. Manifests can be written by hand or generated with write_manifest_from_dir.
read_manifest(manifest, base_url = NULL)read_manifest(manifest, base_url = NULL)
manifest |
string or data.frame. The manifest to read: either the path to a CSV file, or a data.frame that is used directly. Comment lines in a CSV manifest that start with |
base_url |
string or NULL. A base URL used to derive the download URL for all files that do not have an explicit URL. For such files, the URL is set to |
The manifest must contain a column named path, giving the file path relative to the package cache directory, using / as separator. Two further columns are optional:
url: The remote URL to download the file from. If empty (or missing), it is derived from base_url (see above).
md5: The MD5 checksum of the file, used to verify file integrity. If empty (or missing), no integrity check is performed for that file (only its existence is checked).
Additional columns are allowed and ignored.
data.frame with the columns path, url and md5. The url column never contains missing values after this function ran: either it was present in the input, or it was derived from base_url and path. The md5 column may contain NA for files without a checksum.
Delete all the given files from the package cache.
remove_cached_files(pkg_info, relative_filenames)remove_cached_files(pkg_info, relative_filenames)
pkg_info |
named list. Package identifier, see get_pkg_info() on how to get one. |
relative_filenames |
vector of strings. A vector of filenames, relative to the package cache. |
logical vector. For each file, whether it was deleted. Note that files which did not exist were not deleted! You should check the results using 'files_available'.
pkg_info = get_pkg_info("mypackage") deleted = remove_cached_files(pkg_info, "some_file.txt")pkg_info = get_pkg_info("mypackage") deleted = remove_cached_files(pkg_info, "some_file.txt")
Create a manifest (CSV file) that describes all files in the given directory. This is meant to remove the tedious manual work of adding many files to a package: put the files into a directory (subdirectories are preserved), run this function once, and a ready-to-use manifest is written. The MD5 checksum of every file is computed automatically.
write_manifest_from_dir(dir, out, url_base = NULL)write_manifest_from_dir(dir, out, url_base = NULL)
dir |
string. The directory containing the local files to describe. Files in subdirectories are included, and their relative paths (using '/' as separator) become the |
out |
string. The path of the CSV file to write the manifest to. Typically something like |
url_base |
string or NULL. If given, the |
The generated manifest as a data.frame, invisibly.
## Not run: manifest = write_manifest_from_dir("~/mydata", "inst/extdata/files.csv", url_base = "https://example.com/data/") ## End(Not run)## Not run: manifest = write_manifest_from_dir("~/mydata", "inst/extdata/files.csv", url_base = "https://example.com/data/") ## End(Not run)