Learn R Programming

SpaDES (version 1.0.1)

loadFiles: Load simulation objects according to filelist

Description

This function takes the filelist argument in the simList object and loads all the files using the identified functions and arguments. How to load various types of files in R. How to load various types of files in R.

Usage

loadFiles(sim, filelist, ...)

## S3 method for class 'simList,missing':
loadFiles(sim, filelist, ...)

## S3 method for class 'missing,ANY':
loadFiles(sim, filelist, ...)

## S3 method for class 'missing,missing':
loadFiles(sim, filelist, ...)

.fileExtensions()

.saveFileExtensions()

Arguments

sim
simList object.
filelist
list or data.frame to call loadFiles directly from the filelist as described in Details
...
Additional arguments.

Details

In the filelist object, either a list or a data.frame, there will be minimally a column called "files". All other columns are optional. Other optional columns are: - objectName: a character string indicating the name of the object once the file is loaded. Default is to use the file names, with file extension removed. - package: a character string indicating the package that the function is found in. There is no default. - functions: a character string indicating the function to be used to load the file. Default is to use the mapping between file extensions in the .fileExtensions function and the actual file extensions. - intervals: a numeric indicating the interval between repeated loading of the same file. This should be NA or the column absent if the file is only loaded once. Default is absent, so files are loaded only at start in the simList. - loadTime: a numeric indicating when the file should be loaded. Defaults to simTime=0,but this can be any time. The loading will be scheduled to occur at the "loadTime", whatever that is. If the same file is to loaded many times, but not at a regular interval, then there should be separate line, with a unique loadTime for each. - arguments: is a list of lists of named arguments, one list for each loading function. For example, if raster is a loading function, arguments = list(native = TRUE). If there is only one list, then it is assumed to apply to all load attempts and will be repeated for each load function.

Examples

Run this code
# Load random maps included with package
filelist <- data.frame(
    files=dir(file.path(find.package("SpaDES", quiet=FALSE), "maps"),
    full.names=TRUE, pattern="tif"), functions="rasterToMemory", package="SpaDES"
    )

times <- list(start=0, end=3)
parameters <- list(.globals=list(stackName="landscape"))
modules <- list("randomLandscapes", "caribouMovement")
paths <- list(moduleName=system.file("sampleModules", package="SpaDES"))
mySim <- simInit(times=times, params=parameters, modules=modules, paths=paths,
inputs=filelist)
ls(mySim)

sim1 <- loadFiles(filelist=filelist)
clearPlot()
Plot(sim1$DEM)

# Second, more sophisticated. All maps loaded at time = 0, and the last one is reloaded
#  at time = 10 and 20 (via "intervals").
# Also, pass the single argument as a list to all functions...
#  specifically, when add "native = TRUE" as an argument to the raster function
files = dir(file.path(find.package("SpaDES", quiet = FALSE), "maps"),
        full.names=TRUE, pattern= "tif")
arguments = I(rep(list(native=TRUE), length(files)))
filelist = data.frame(
   files = files,
   functions="raster::raster",
   objectName = NA,
   arguments = arguments,
   loadTime = 0,
   intervals = c(rep(NA, length(files)-1), 10)
   )

sim2 <- loadFiles(filelist=filelist)
end(sim2) <- 20
sim2 <- spades(sim2)

Run the code above in your browser using DataLab