These functions are one of two mechanisms to add the information about which
input files to load in a spades
call and the information about which
output files to save.
The other way is to pass them as arguments to a simInit
call.
inputArgs
and outputArgs
are ways to specify any
arguments that are needed for file loading and file saving. This
is still somewhat experimental.
inputs(object)# S4 method for .simList
inputs(object)
inputs(object) <- value
# S4 method for .simList
inputs(object) <- value
outputs(object)
# S4 method for .simList
outputs(object)
outputs(object) <- value
# S4 method for .simList
outputs(object) <- value
inputArgs(object)
# S4 method for .simList
inputArgs(object)
inputArgs(object) <- value
# S4 method for .simList
inputArgs(object) <- value
outputArgs(object)
# S4 method for .simList
outputArgs(object)
outputArgs(object) <- value
# S4 method for .simList
outputArgs(object) <- value
A simList
simulation object.
The object to be stored at the slot. See Details.
Returns or sets the value(s) of the input
or output
slots
in the simList
object.
inputs
accepts a data.frame, with up to 7 columns.
Columns are:
file |
required, a character string indicating the file path. There is no default. |
|
optional, character string indicating the name of the object
that the loaded file will be assigned to in the simList . This object
can therefore be accessed with sim$xxx in any module, where
objectName = "xxx" . Defaults to the filename without file extension or
directory information. |
|
optional, a character string indicating the function to use to
load that file. Defaults to the known extentions in SpaDES (found by
examining .fileExtensions() ). The package and fun can be
jointly specified here as "packageName::functionName" , e.g.,
"raster::raster" . |
|
optional character string indicating the package in
which to find the fun ); |
|
optional numeric, indicating when in simulation time the file
should be loaded. The default is the highest priority at start(sim) ,
i.e., at the very start. |
|
optional numeric, indicating at what interval should this same
exact file be reloaded from disk, e.g,. 10 would mean every 10 time units. The
default is NA or no interval, i.e, load the file only once as described in
loadTime |
|
is a list of lists of named arguments, one list for each
fun . For example, if fun="raster" , arguments = list(native = TRUE)
will pass the argument "native = TRUE" to raster. If there is only one list,
then it is assumed to apply to all files and will be recycled as per normal R
rules of recycling for each fun . |
Currently, only file
is required. All others will be filled with defaults
if not specified.
See the modules vignette for more details (browseVignettes("SpaDES")
).
outputs
accepts a data.frame similar to the inputs
data.frame, but
with up to 6 columns.
objectName |
required, character string indicating the name of the object
in the simList that will be saved to disk (without the sim$ prefix). |
|
optional, a character string indicating the file path to save to.
The default is to concatenate objectName with the model timeunit and
saveTime , separated by underscore, "_". So a default filename would be
"Fires_year1.rds" |
|
optional, a character string indicating the function to use to
save that file. The default is saveRDS |
|
optional character string indicating the package in
which to find the fun ); |
|
optional numeric, indicating when in simulation time the file
should be saved. The default is the lowest priority at end(sim) ,
i.e., at the very end. |
|
is a list of lists of named arguments, one list for each
fun . For example, if fun="write.csv" ,
arguments = list(row.names = TRUE)
will pass the argument "row.names = TRUE" to write.csv If there is only one list,
then it is assumed to apply to all files and will be recycled as per normal R
rules of recycling for each fun . |
See the modules vignette for more details (browseVignettes("SpaDES")
).
Accessor functions for the inputs
and outputs
slots in a
simList
object.
SpaDES
, specifically the section 1.2.2 on loading and saving.
Other functions to access elements of a simList
object: .addDepends
,
doEvent.checkpoint
, envir
,
events
, globals
,
ls.simList
, ls.str.simList
,
modules
, objs
,
packages
, params
,
paths
, progressInterval
,
times
# NOT RUN {
#######################
# inputs
#######################
# Start with a basic empty simList
sim <- simInit()
test <- 1:10
library(igraph) # for %>%
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 = "SpaDES"),
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 = "SpaDES"),
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)
}
# 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="second10.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