filer: Get file path transparently for a possibly-gzip-compressed...

Description Usage Arguments Details Value Examples

Description

Returns a (relative but otherwise complete) file path from its base name and optional extensions (default .txt). The main goal is to handle compressed files transparently, without having to know ahead of time if the compressed or uncompressed version is the one that is present. If gzip=TRUE, .gz extension gets added. If exists=TRUE, existing uncompressed or compressed file path is returned, or stops if file is missing. If logical=TRUE, returns a boolean that indicates if file exists or not, testing both uncompressed and compressed versions.

Usage

1
2
filer(name, ext = "txt", gzip = FALSE, exists = FALSE,
  logical = FALSE)

Arguments

name

Base name of file (mandatory).

ext

Extension (default txt). Period between name and ext gets added automatically unless ext==''.

gzip

If true, adds .gz extension after usual extension.

exists

If true, checks that file exists; if file did not exist, checks if compressed version exists (returns that path in that case). If gzip=TRUE, then only the compressed version is checked for existence.

logical

If true, function will return a boolean for whether file exists or not (regular or compressed).

Details

Behavior when multiple modifiers are TRUE: logical=TRUE overrides exists=TRUE (returns boolean, non-fatal). gzip=TRUE restricts file existence test to compressed file when either logical=TRUE or exists=TRUE.

Value

If exists=FALSE and logical=FALSE, returns the desired relative file path, with extensions added as requested (no existence tests). If exists=TRUE, returns the path of the file that exists (adds .gz extension if only that file exists, even if gzip=FALSE), stopping (fatally) if the file is missing. If logical=TRUE, returns TRUE if file (regular or compressed) exists, FALSE otherwise.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# examples that simply construct file path (without existence tests)
filer('myfile') # returns "myfile.txt"
filer('myfile', 'pdf') # returns "myfile.pdf"
filer('myfile', '') # returns "myfile" (no extension)
filer('myfile', gzip=TRUE) # returns "myfile.txt.gz"
filer('myfile', 'yml', gzip=TRUE) # returns "myfile.yml.gz"
filer('myfile', '', gzip=TRUE) # returns "myfile.gz"
# no special treatment for periods in name (not assumed to be existing extensions)
filer('myfile.blah') # returns "myfile.blah.txt"
filer('myfile.blah', '') # returns "myfile.blah" (no added extension)

# test that file exists
# returns TRUE if myfile.txt or myfile.txt.gz exist, FALSE otherwise
filer('myfile', logical=TRUE)
# returns TRUE if myfile.txt.gz exist, FALSE otherwise (even if myfile.txt exists)
filer('myfile', gzip=TRUE, logical=TRUE)

## Not run: 
### (these examples are fatal when the file in question doesn't exist)

# returns myfile.txt if it exists,
# or myfile.txt.gz if that exist,
# or stops with an informative message if file does not exist
filer('myfile', exists=TRUE)

# returns myfile.txt.gz if it exist,
# or stops with an informative message if file does not exist
# (even if myfile.txt exists)
filer('myfile', gzip=TRUE, exists=TRUE)

## End(Not run)

alexviiia/filer documentation built on May 6, 2019, 7:05 p.m.