ifMissing {Orcs} | R Documentation |
Take Measures in Case of Nonexisting Target Files
Description
If a target file already exists, it is simply being imported into R. However,
if the specified target file does not exist, it is first created by a
user-defined function and subsequently returned, thus rendering explicit
calls to file.exists()
unnecessary.
Usage
ifMissing(ofl, fun0 = terra::rast, fun1 = terra::writeRaster, arg1, ...)
Arguments
ofl |
Target file name as |
fun0 |
If 'ofl' exists, |
fun1 |
If 'ofl' does not exist, |
arg1 |
Argument in 'fun1' (as |
... |
Additional arguments passed to 'fun0' and 'fun1'. |
Value
If 'ofl' has already existed, the contents of 'ofl' derived from 'fun0'; else the output resultant from 'fun1'.
Author(s)
Florian Detsch
See Also
Examples
# simply import existing file
logo <- system.file("ex/logo.tif", package = "terra")
s <- ifMissing(logo)
# create nonexisting file and import it afterwards
logo2 <- file.path(tempdir(), "rlogo.tif")
s2 <- ifMissing(logo2, arg1 = "filename", x = s, datatype = "INT1U")
# this also works with text files and more sophisticated custom functions
fun = function(x, file = "", ...) {
write.csv(x, file, ...)
read.csv(file)
}
data(iris)
ofl <- file.path(tempdir(), "iris.csv")
iris2 <- ifMissing(ofl, fun1 = fun, x = iris, file = ofl, quote = FALSE, row.names = FALSE)