knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The filer
R package provides functions for easily constructing paths and querying for files that may or may not be compressed (with .gz extension).
You can install filer from GitHub using the R commands:
library(devtools) install_github("alexviiia/filer")
The default mode of filer
is to construct file path strings:
# load package library(filer) # a file with standard .txt extension: filer('myfile') # change extension filer('myfile', 'pdf') # no extension filer('myfile', '') # compressed version filer('myfile', gzip=TRUE) # compressed and different extension filer('myfile', 'yml', gzip=TRUE) # compressed but no other extension filer('myfile', '', gzip=TRUE) # no special treatment for periods in name (not assumed to be existing extensions) filer('myfile.blah') filer('myfile.blah', '')
There are two modes that make this much more useful in practice. Both of these test for file existence, looking for compressed files when the regular versions don't exist, and let you know when neither files are found.
Let's quickly create a temporary file that really exists, so you can see what happens when you test for it.
# "base" of file to test for (without extensions) base <- file.path( tempdir(), 'myfile') # full path of file, with standard plus compressed extension fileTmp <- paste0(base, '.txt.gz') # create file for this example file.create(fileTmp)
The first mode is the "logical" mode, which returns a boolean for whether a file exists.
Note that if we're testing for file.txt
, this function returns TRUE
if either file.txt
or file.txt.gz
exist, and FALSE
only if both are missing.
# this file 'fakeFile.txt' doesn't exist filer('fakeFile', logical=TRUE) # test for file that does exist (exclude extensions). # Although base.txt doesn't exist, compressed version base.txt.gz does, so this returns TRUE! filer(base, logical=TRUE)
The second mode is the "exists" mode, which returns either the uncompressed or compressed file path if it exists, but stops with an informative error message if the file does not exist. This is very useful when we want to load a file, we're not sure if it was compressed or not, but definitely want things to stop if the file cannot be found (in either of uncompressed or compressed versions).
# this file 'fakeFile.txt' doesn't exist filer('fakeFile', exists=TRUE) # test for file that does exist (exclude extensions). # Although base.txt doesn't exist, compressed version base.txt.gz does (returns this version) filer(base, exists=TRUE)
Lastly, let's cleanup your file system.
# remove temporary file file.remove(fileTmp)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.