ifMissing: Take measures in case of nonexisting target files

Description Usage Arguments Value Author(s) See Also Examples

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

1
2
ifMissing(ofl, fun0 = raster::brick, fun1 = raster::writeRaster, arg1,
  ...)

Arguments

ofl

Target file name as character.

fun0

If 'ofl' exists, function to be applied to it (defaults to brick).

fun1

If 'ofl' does not exist, function used to create it (defaults to writeRaster).

arg1

Argument in 'fun1' (as character) that corresponds to 'ofl', e.g. filename in writeRaster or file in write.table. If missing (default), the target file name passed to 'fun1' needs to be explicitly included via '...'.

...

Additional arguments passed to 'fun0,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

file.exists, do.call.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# simply import existing file
logo <- system.file("external/rlogo.grd", package = "raster")
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)

Orcs documentation built on May 2, 2019, 2:37 a.m.