# NOT RUN {
#######################
# inputs
#######################
# Start with a basic empty simList
sim <- simInit()
test <- 1:10
library(igraph) # for %>%
library(reproducible) # for checkPath
tmpdir <- file.path(tempdir(), "inputs") %>% checkPath(create = TRUE)
tmpFile <- file.path(tmpdir, "test.rds")
saveRDS(test, file = tmpFile)
inputs(sim) <- data.frame(file = tmpFile) # using only required column, "file"
inputs(sim) # see that it is not yet loaded, but when it is scheduled to be loaded
simOut <- spades(sim)
inputs(simOut) # confirm it was loaded
simOut$test
# can put data.frame for inputs directly inside simInit call
allTifs <- dir(system.file("maps", package = "quickPlot"),
full.names = TRUE, pattern = "tif")
# next: .objectNames are taken from the filenames (without the extension)
# This will load all 5 tifs in the SpaDES sample directory, using
# the raster fuction in the raster package, all at time = 0
if (require("rgdal", quietly = TRUE)) {
sim <- simInit(
inputs = data.frame(
files = allTifs,
functions = "raster",
package = "raster",
loadTime = 0,
stringsAsFactors = FALSE)
)
##############################
#A fully described inputs object, including arguments:
files <- dir(system.file("maps", package = "quickPlot"),
full.names = TRUE, pattern = "tif")
# arguments must be a list of lists. This may require I() to keep it as a list
# once it gets coerced into the data.frame.
arguments = I(rep(list(native = TRUE), length(files)))
filelist = data.frame(
objectName = paste0("Maps", 1:5),
files = files,
functions = "raster::raster",
arguments = arguments,
loadTime = 0,
intervals = c(rep(NA, length(files) - 1), 10)
)
inputs(sim) <- filelist
spades(sim)
}
# Example showing loading multiple objects from global environment onto the
# same object in the simList, but at different load times
a1 <- 1
a2 <- 2
# Note arguments must be a list of NROW(inputs), with each element itself being a list,
# which is passed to do.call(fun[x], arguments[[x]]), where x is row number, one at a time
args <- lapply(1:2, function(x) {
list(x = paste0("a", x),
envir = environment()) # may be necessary to specify in which envir a1, a2
# are located, if not in an interactive sessino
})
inputs <- data.frame(objectName = "a", loadTime = 1:2, fun = "base::get", arguments = I(args))
a <- simInit(inputs = inputs, times = list(start = 0, end = 1))
a <- spades(a)
identical(a1, a$a)
end(a) <- 3
a <- spades(a) # different object (a2) loaded onto a$a
identical(a2, a$a)
# Clean up after
unlink(tmpdir, recursive = TRUE)
#######################
# outputs
#######################
library(igraph) # for %>%
tmpdir <- file.path(tempdir(), "outputs") %>% checkPath(create = TRUE)
tmpFile <- file.path(tmpdir, "temp.rds")
tempObj <- 1:10
# Can add data.frame of outputs directly into simInit call
sim <- simInit(objects = c("tempObj"),
outputs = data.frame(objectName = "tempObj"),
paths = list(outputPath = tmpdir))
outputs(sim) # To see what will be saved, when, what filename
sim <- spades(sim)
outputs(sim) # To see that it was saved, when, what filename
# Also can add using assignment after a simList object has been made
sim <- simInit(objects = c("tempObj"), paths = list(outputPath = tmpdir))
outputs(sim) <- data.frame(objectName = "tempObj", saveTime = 1:10)
sim <- spades(sim)
outputs(sim) # To see that it was saved, when, what filename.
# can do highly variable saving
tempObj2 <- paste("val",1:10)
df1 <- data.frame(col1 = tempObj, col2 = tempObj2)
sim <- simInit(objects = c("tempObj", "tempObj2", "df1"),
paths=list(outputPath = tmpdir))
outputs(sim) = data.frame(
objectName = c(rep("tempObj", 2), rep("tempObj2", 3), "df1"),
saveTime = c(c(1,4), c(2,6,7), end(sim)),
fun = c(rep("saveRDS", 5), "write.csv"),
package = c(rep("base", 5), "utils"),
stringsAsFactors = FALSE)
# since write.csv has a default of adding a column, x, with rownames, must add additional
# argument for 6th row in data.frame (corresponding to the write.csv function)
outputArgs(sim)[[6]] <- list(row.names=FALSE)
sim <- spades(sim)
outputs(sim)
# read one back in just to test it all worked as planned
newObj <- read.csv(dir(tmpdir, pattern = "year10.csv", full.name = TRUE))
newObj
# using saving with SpaDES-aware methods
# To see current ones SpaDES can do
.saveFileExtensions()
library(raster)
if (require(rgdal)) {
ras <- raster(ncol = 4, nrow = 5)
ras[] <- 1:20
sim <- simInit(objects = c("ras"), paths = list(outputPath = tmpdir))
outputs(sim) = data.frame(
file = "test",
fun = "writeRaster",
package = "raster",
objectName = "ras",
stringsAsFactors = FALSE)
outputArgs(sim)[[1]] <- list(format = "GTiff") # see ?raster::writeFormats
simOut <- spades(sim)
outputs(simOut)
newRas <- raster(dir(tmpdir, full.name = TRUE, pattern = ".tif"))
all.equal(newRas, ras) # Should be TRUE
}
# Clean up after
unlink(tmpdir, recursive = TRUE)
# }
Run the code above in your browser using DataLab