How to load various types of files in R.

This function has two roles:

  1. to proceed with the loading of files that are in a simList; or

  2. as a shortcut to simInit(inputs = filelist).

A data.frame with information on how to load various types of files in R, containing the columns:

  • exts: the file extension;

  • fun: the function to use for files with this file extension;

  • package: the package from which to load fun.

.fileExtensions()

loadFiles(sim, filelist, ...)

# S4 method for simList,missing
loadFiles(sim, filelist, ...)

# S4 method for missing,ANY
loadFiles(sim, filelist, ...)

# S4 method for missing,missing
loadFiles(sim, filelist, ...)

.saveFileExtensions()

Arguments

sim

simList object.

filelist

list or data.frame to call loadFiles directly from the filelist as described in Details

...

Additional arguments.

Value

data.frame of file extension, package, and function mappings

the modified sim, invisibly.

data.frame

Note

Generally not intended to be used by users.

See also

Author

Eliot McIntire and Alex Chubaty

Examples

# \donttest{
library(SpaDES.core)

# Load random maps included with package
filelist <- data.frame(
  files = dir(getMapPath(tempdir()), full.names = TRUE),
  functions = "rasterToMemory",
  package = "SpaDES.core"
)
sim1 <- loadFiles(filelist = filelist) # loads all the maps to sim1 simList
#> Setting:
#>   options(
#>     reproducible.cachePath = '/tmp/RtmpoLaiAE/reproducible/cache'
#>     spades.inputPath = '/tmp/RtmpoLaiAE/SpaDES/inputs'
#>     spades.outputPath = '/tmp/RtmpoLaiAE/SpaDES/outputs'
#>     spades.modulePath = '/tmp/RtmpoLaiAE/SpaDES/modules'
#>     spades.scratchPath = '/tmp/RtmpoLaiAE/SpaDES/scratch'
#>   )
#> Apr25 18:01:06 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Apr25 18:01:06 simInit DEM read from /tmp/RtmpoLaiAE/maps/DEM.tif using rasterToMemory
#> Apr25 18:01:06 simInit forestAge read from /tmp/RtmpoLaiAE/maps/forestAge.tif using rasterToMemory
#> Apr25 18:01:06 simInit forestCover read from /tmp/RtmpoLaiAE/maps/forestCover.tif using rasterToMemory
#> Apr25 18:01:06 simInit habitatQuality read from /tmp/RtmpoLaiAE/maps/habitatQuality.tif using rasterToMemory
#> Apr25 18:01:06 simInit percentPine read from /tmp/RtmpoLaiAE/maps/percentPine.tif using rasterToMemory
#> Elpsed time for simInit: 0.1570084 secs

# 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(getMapPath(tempdir()), full.names = TRUE)
arguments <- I(rep(list(lyrs = 1), length(files)))
filelist <- data.frame(
   files = files,
   functions = "terra::rast",
   objectName = NA,
   arguments = arguments,
   loadTime = 0,
   intervals = c(rep(NA, length(files)-1), 10)
)

sim2 <- loadFiles(filelist = filelist) # only does the time = 0 loading; see next
#> Setting:
#>   options(
#>     reproducible.cachePath = '/tmp/RtmpoLaiAE/reproducible/cache'
#>     spades.inputPath = '/tmp/RtmpoLaiAE/SpaDES/inputs'
#>     spades.outputPath = '/tmp/RtmpoLaiAE/SpaDES/outputs'
#>     spades.modulePath = '/tmp/RtmpoLaiAE/SpaDES/modules'
#>     spades.scratchPath = '/tmp/RtmpoLaiAE/SpaDES/scratch'
#>   )
#> Apr25 18:01:06 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Apr25 18:01:06 simInit DEM read from /tmp/RtmpoLaiAE/maps/DEM.tif using rast(inMemory=FALSE)
#> Apr25 18:01:06 simInit forestAge read from /tmp/RtmpoLaiAE/maps/forestAge.tif using rast(inMemory=FALSE)
#> Apr25 18:01:06 simInit forestCover read from /tmp/RtmpoLaiAE/maps/forestCover.tif using rast(inMemory=FALSE)
#> Apr25 18:01:06 simInit habitatQuality read from /tmp/RtmpoLaiAE/maps/habitatQuality.tif using rast(inMemory=FALSE)
#> Apr25 18:01:06 simInit percentPine read from /tmp/RtmpoLaiAE/maps/percentPine.tif using rast(inMemory=FALSE)
#> Elpsed time for simInit: 0.05944967 secs
end(sim2) <- 10
sim2 <- spades(sim2) # loads the object at time 10
#> Apr25 18:01:06 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Apr25 18:01:06 chckpn:init total elpsd: 0.06 secs | 0 checkpoint init 0
#> Apr25 18:01:06 save  :init total elpsd: 0.062 secs | 0 save init 0
#> Apr25 18:01:06 prgrss:init total elpsd: 0.064 secs | 0 progress init 0
#> Apr25 18:01:06 load  :init total elpsd: 0.066 secs | 0 load init 0
#> Apr25 18:01:06 load  :inputs total elpsd: 0.068 secs | 10 load inputs 0
#> Apr25 18:01:06 load  :inputs percentPine read from /tmp/RtmpoLaiAE/maps/percentPine.tif using rast(inMemory=FALSE)
#> Apr25 18:01:06 load  :inputs    at time 10
#> simList saved in
#> SpaDES.core:::.pkgEnv$.sim
#> It will be deleted at next spades() call.

# if we extend the end time and continue running, it will load an object scheduled
#  at time = 10, and it will also schedule a new object loading at 20 because
#  interval = 10
end(sim2) <- 20
sim2 <- spades(sim2) # loads the percentPine map 2 more times, once at 10, once at 20
#> Apr25 18:01:06 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Apr25 18:01:06 load  :inputs total elpsd: 0.086 secs | 20 load inputs 0
#> Apr25 18:01:06 load  :inputs percentPine read from /tmp/RtmpoLaiAE/maps/percentPine.tif using rast(inMemory=FALSE)
#> Apr25 18:01:06 load  :inputs    at time 20
#> simList saved in
#> SpaDES.core:::.pkgEnv$.sim
#> It will be deleted at next spades() call.
# }