tests/testthat/test-ifMissing.R

context("ifMissing")

## raster files
logo = system.file("external/rlogo.grd", package = "raster")

ofl = file.path(tmp <- tempdir(), "rlogo.tif")
if (file.exists(ofl)) jnk = file.remove(ofl)

test_that("nonexisting raster file is created", {
  
  s = ifMissing(logo)
  expect_is(s, "RasterBrick")

  s2 = ifMissing(ofl, arg1 = "filename", x = s, datatype = "INT1U")  
  expect_is(s, "RasterBrick")
  
  s3 = ifMissing(ofl)
  expect_is(s, "RasterBrick")
  
  jnk = file.remove(ofl)
})

## text files
data(iris)

ofl = file.path(tmp, "iris.csv")
if (file.exists(ofl)) jnk = file.remove(ofl)

test_that("nonexisting text file is created", {
  
  jnk = ifMissing(ofl, fun1 = write.csv, x = iris, file = ofl, row.names = FALSE)
  expect_null(jnk)
  
  dat = ifMissing(ofl, fun0 = read.csv)
  expect_equal(dat, iris)
  
  jnk = file.remove(ofl)
  expect_true(jnk)
  
  fun = function(x, file = "", ...) {
    write.csv(x, file, ...)
    read.csv(file)
  }
  dat = ifMissing(ofl, fun1 = fun, arg1 = "file", x = iris, quote = FALSE, row.names = FALSE)
  expect_equal(dat, iris)
  
  jnk = file.remove(ofl)
})

Try the Orcs package in your browser

Any scripts or data that you put into this service are public.

Orcs documentation built on April 18, 2019, 9:04 a.m.