maybe_process {dlr}R Documentation

Process a File if Necessary

Description

Sometimes you just need to get a processed file to a particular location, without reading the data. For example, you might need to download a lookup table used by various functions in a package, independent of a particular function call that needs the data. This function does the processing if it hasn't already been done.

Usage

maybe_process(
  source_path,
  target_path,
  process_f = readRDS,
  process_args = NULL,
  write_f = saveRDS,
  write_args = NULL,
  force_process = FALSE
)

Arguments

source_path

Character scalar; the path to the raw file. Paths starting with http://, http://, http://, or http:// will be downloaded to a temp file if the processed version is not already available.

target_path

Character scalar; the path where the processed version of the file should be stored.

process_f

A function or one-sided formula to use to process the source file. source_path will be passed as the first argument to this function. Defaults to read_f.

process_args

An optional list of additional arguments to process_f.

write_f

A function or one-sided formula to use to save the processed file. The processed object will be passed as the first argument to this function, and target_path will be passed as the second argument. Defaults to saveRDS.

write_args

An optional list of additional arguments to write_f.

force_process

A logical scalar indicating whether we should process the source file even if the target already exists. This can be particularly useful if you wish to redownload a file.

Value

The normalized target_path.

Examples

if (interactive()) {
  temp_filename <- tempfile()
  maybe_process(
    "https://query.data.world/s/owqxojjiphaypjmlxldsp566lck7co",
    target_path = temp_filename,
    process_f = read.csv
  )

  unlink(temp_filename)
}

[Package dlr version 1.0.1 Index]