fread_plus_date {libbib} | R Documentation |
Read a file and set a special libbib date attribute
Description
Takes a file name, reads it with data.table::fread
, and
sets an attribute called lb.date
with a date extracted
from the file name.
Usage
fread_plus_date(fname, allow.fallback.date = TRUE, ...)
Arguments
fname |
The file name to read |
allow.fallback.date |
A logical indicating whether, if no
matching file name with a date is found,
to use today's date as the date attribute.
Default is |
... |
Arbitrary arguments to use with |
Details
The file name can be one with a valid ISO 8601 date (yyyy-mm-dd) already in it, or it can be a file name with the date elided.
For example, if there is a file you'd like to read on your disk called "iris-2021-05-08.csv", you can call this function with either "iris.csv" or "iris-2021-05-08.csv" as the file name.
When you call this function with a file name without an ISO 8601 date (e.g. "iris.csv.gz"), the file name extension ".csv.gz" is removed and the function looks for a file name beginning with "iris", a date, and the file extension. The file extension is considered to be anything after the first period in the base name. For example, if the file name given is "./my.data/iris.csv.gz", the extension is ".csv.gz". This means no period can be present in the base file name (after any directories) with the exception of the file extension.
If you call this function with "iris.csv" and there is no file name
with an ISO 8601 date appended to that file name on your disk, and
allow.fallback.date
is TRUE
, then the lb.date
attribute is set to the current date.
Value
A data.table
with an attribute called lb.date
set
Examples
## Not run:
# there's a file called "iris-2021-05-08.csv" on disk
dat <- fread_plus_date("iris.csv")
attribute(dat)$lb.date
# [1] "2021-05-08
# can also read the full file name
dat <- fread_plus_date("iris-2021-05-08.csv")
attribute(dat)$lb.date
# [1] "2021-05-08
## End(Not run)