Title: | Easy Management of File Names |
---|---|
Description: | Create descriptive file names with ease. New file names are automatically (but optionally) time stamped and placed in date stamped directories. Streamline your analysis pipeline with input and output file names that have informative tags and proper file extensions. |
Authors: | David J. H. Shih |
Maintainer: | David J. H. Shih <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.4 |
Built: | 2025-01-26 04:25:10 UTC |
Source: | https://bitbucket.org/djhshih/filenamer |
This function coerces a filename
into a character.
## S3 method for class 'filename' as.character(x, tag.char = NULL, simplify = FALSE, ...)
## S3 method for class 'filename' as.character(x, tag.char = NULL, simplify = FALSE, ...)
x |
a |
tag.char |
character to delimit tags, defaults to |
simplify |
if |
... |
unused arguments |
a character
vector
x <- "data_post_2011-01-02.txt" fn <- as.filename(x) print(as.character(fn))
x <- "data_post_2011-01-02.txt" fn <- as.filename(x) print(as.character(fn))
This function coerces an object into a filename
, if possible.
as.filename(x, ...) ## S3 method for class 'filename' as.filename(x, ...) ## S3 method for class 'character' as.filename(x, tag.char = NULL, ...)
as.filename(x, ...) ## S3 method for class 'filename' as.filename(x, ...) ## S3 method for class 'character' as.filename(x, tag.char = NULL, ...)
x |
a |
... |
other arguments |
tag.char |
character to delimit tags, defaults to |
a filename
object
fn <- as.filename("data_raw_2011-01-01.txt") str(fn)
fn <- as.filename("data_raw_2011-01-01.txt") str(fn)
This function creates a filename
object with a file path, tags,
extensions, date stamp or date-time stamp.
filename( x, path = NULL, tag = NULL, ext = NULL, date = NULL, time = NULL, subdir = TRUE )
filename( x, path = NULL, tag = NULL, ext = NULL, date = NULL, time = NULL, subdir = TRUE )
x |
file name stem |
path |
path to the file |
tag |
tags for the file name |
ext |
file extension |
date |
date stamp ( |
time |
time stamp ( |
subdir |
whether to append a date/time stamped subdirectory to path |
The date
and time
parameters can be specified as
character
vectors or date/time objects. If time
is given as a POSIXct
, it will override date
.
If these parameters are both NULL
, automated date and time stamping
may be done and is controlled by getOption("filenamer.timestamp")
.
If this option is NULL
, 0, or less,
no date or time stamping will be done;
if it is 1, only date stamping will be done;
if it is 2 or greater, date-time stamping will be done (default).
Set date
or time
to NA
to suppress date or
time stamping for a particular filename
.
Stamps are shown in ISO 8601 date format (
(
platforms and are thus omitted; hyphens are omitted from date-time stamps
for brevity.
By default, a date stamped subdirectory is appended to the file path.
To disable this behaviour, set subdir
to FALSE
or disable
path stamping globally by options(filenamer.path.timestamp = 0)
.
This option is similar to filenamer.timestamp
above.
a filename
object
# file name is date-time stamped and put in subdirectory by default fn <- filename("data", tag="qc", ext="txt") print(as.character(fn)) # disable date-time stamping and subdirectory insertion fn2 <- filename("data", tag="qc", date=NA, time=NA, subdir=FALSE) print(as.character(fn2)) # creating a new file name from an existing one yields a new time stamp fn3 <- filename(fn) print(as.character(fn3))
# file name is date-time stamped and put in subdirectory by default fn <- filename("data", tag="qc", ext="txt") print(as.character(fn)) # disable date-time stamping and subdirectory insertion fn2 <- filename("data", tag="qc", date=NA, time=NA, subdir=FALSE) print(as.character(fn2)) # creating a new file name from an existing one yields a new time stamp fn3 <- filename(fn) print(as.character(fn3))
This function inserts a tag or extension into a file name. It can also replace an element of a file name.
insert(x, ...) ## S3 method for class 'filename' insert( x, tag = NULL, tag.pos = NULL, ext = NULL, ext.pos = NULL, replace = FALSE, ... ) ## S3 method for class 'character' insert(x, ...)
insert(x, ...) ## S3 method for class 'filename' insert( x, tag = NULL, tag.pos = NULL, ext = NULL, ext.pos = NULL, replace = FALSE, ... ) ## S3 method for class 'character' insert(x, ...)
x |
file name ( |
... |
unused arguments |
tag |
one or more file name tags to insert |
tag.pos |
position at which to insert tag
( |
ext |
one or more file extension tags to insert |
ext.pos |
position at which to insert extension
( |
replace |
if |
By default, tags are inserted at the ultimate position and extensions
at the penultimate position, if possible.
(That is, the final file extension will not change, unless the insertion
position is specified otherwise or the original file name had no extension.)
If replace
is TRUE
, the tag at the indicated position is
replaced by the new tag instead.
modified object of the original type
f <- as.filename("data_expr_2014-05-01.tsv") # new file name with inserted tags for saving normalized data g <- insert(f, tag=c("mod", "norm")) print(as.character(g)) # new file name with inserted extension for saving sorted data h <- insert(f, ext="sorted") print(as.character(h)) # new file name with different extension for saving in different format i <- insert(f, ext="csv", replace=TRUE) print(as.character(i)) # insert another tag j <- insert(g, tag="qc", tag.pos=2) print(as.character(j))
f <- as.filename("data_expr_2014-05-01.tsv") # new file name with inserted tags for saving normalized data g <- insert(f, tag=c("mod", "norm")) print(as.character(g)) # new file name with inserted extension for saving sorted data h <- insert(f, ext="sorted") print(as.character(h)) # new file name with different extension for saving in different format i <- insert(f, ext="csv", replace=TRUE) print(as.character(i)) # insert another tag j <- insert(g, tag="qc", tag.pos=2) print(as.character(j))
This function returns TRUE
if its argument is a filename
and FALSE
otherwise.
is.filename(x)
is.filename(x)
x |
object to check |
a logical
value
This function creates directories recursively (as necessary) to the specified file.
make_path(x, ...) ## S3 method for class 'filename' make_path(x, showWarnings = FALSE, recursive = TRUE, ...) ## S3 method for class 'character' make_path(x, ...)
make_path(x, ...) ## S3 method for class 'filename' make_path(x, showWarnings = FALSE, recursive = TRUE, ...) ## S3 method for class 'character' make_path(x, ...)
x |
file name ( |
... |
other arguments passed to |
showWarnings |
whether to show warnings |
recursive |
whether to recursively create all parent directories |
## Not run: # CRAN policy forbids package example to write to current directory, # even inside \dontrun because the user may copy-and-paste and # polute his/her current directory; # in real-world setting, the `tempdir` path prefix is unnecessary x <- file.path(tempdir(), "path/to/file.txt") fn <- as.filename(x) make_path(fn) ## End(Not run)
## Not run: # CRAN policy forbids package example to write to current directory, # even inside \dontrun because the user may copy-and-paste and # polute his/her current directory; # in real-world setting, the `tempdir` path prefix is unnecessary x <- file.path(tempdir(), "path/to/file.txt") fn <- as.filename(x) make_path(fn) ## End(Not run)
This function sets the date stamp in a file name.
set_fdate(x, date)
set_fdate(x, date)
x |
a |
date |
new date stamp ( |
modified object of the original type
x <- "data_norm_2011-01-03.txt" print(set_fdate(x, "2011-01-05"))
x <- "data_norm_2011-01-03.txt" print(set_fdate(x, "2011-01-05"))
This function sets the extension in a file name.
set_fext(x, ext, all)
set_fext(x, ext, all)
x |
a |
ext |
new file extension |
all |
replace the entire extension |
modified object of the original type
x <- "data_norm_2011-01-03.txt" print(set_fext(x, "csv"))
x <- "data_norm_2011-01-03.txt" print(set_fext(x, "csv"))
This function sets the path in a file name.
set_fpath(x, path)
set_fpath(x, path)
x |
a |
path |
new path to file |
modified object of the original type
x <- "path/data_norm.txt" print(set_fpath(x, "new_path"))
x <- "path/data_norm.txt" print(set_fpath(x, "new_path"))
This function sets the time stamp in a file name.
set_ftime(x, time)
set_ftime(x, time)
x |
a |
time |
new time stamp ( |
modified object of the original type
x <- "data_norm_20110103T093015.txt" # change the time to 30 seconds past 2:45 p.m. print(set_ftime(x, "144530")) # to change the date, time must be specified as well print(set_ftime(x, "20110505T101500"))
x <- "data_norm_20110103T093015.txt" # change the time to 30 seconds past 2:45 p.m. print(set_ftime(x, "144530")) # to change the date, time must be specified as well print(set_ftime(x, "20110505T101500"))
This function inserts a tag or extension into a file name and returns
a charcter
vector.
tag(x, ...)
tag(x, ...)
x |
a |
... |
arguments passed to |
a character
vector
x <- "data.txt" y <- tag(x, "qc") print(y) f <- as.filename(x) g <- tag(f, "qc") print(g)
x <- "data.txt" y <- tag(x, "qc") print(y) f <- as.filename(x) g <- tag(f, "qc") print(g)
This function trims extensions from a file name.
trim_ext(x, n)
trim_ext(x, n)
x |
a |
n |
number of extensions to trim off the end |
modified object of the original type
x <- "path/data.txt.gz" print(trim_ext(x))
x <- "path/data.txt.gz" print(trim_ext(x))