Learn R Programming

rangeMapper (version 0.3-0)

rangeMap.save: Save, retrieve and export maps.

Description

Apply a chosen SQL or function at each grid cell, allowing for complex subsetting at both ID (e.g. species) and pixel (e.g. assemblage) levels.

Usage

rangeMap.save(CON, tableName, FUN, biotab, biotrait, subset = list(), path, overwrite = FALSE, cl, ...)

Arguments

CON
an sqlite connection pointing to a valid rangeMapper project.
tableName
name of the table (quoted) to be added to the sqlite database. the prefix ‘MAP’ will be appended to tableName prior to saving.
FUN
the function to be applied to each pixel. If FUN is missing then species richness (species count) is computed.
biotab
character string identifying the ‘BIO’ table to use.
biotrait
character string identifying the ID of the ‘BIO’ table. see bio.save
subset
a named list. See details
path
path to the raster file(quoted) to be imported to the existing project. raster package is required at this step.
overwrite
if TRUE then the table is removed
cl
the number of cores to use or a cluster object defined with makeCluster in package parallel or makeCluster from snow package.
...
when FUN is a function, ... denotes any extra arguments to be passed to it.

Value

TRUE when the MAP was created successfully.

Details

The subset argument accepts a named list. Names refers to ‘BIO’, ‘MAP’ and ‘metadata_rages’ table names while the strings in the list are character strings containing the SQL WHERE clause. The subset can point to either one table type (e.g. list(MAP_species_richness = "species_richness > 500") ) or can point to several table types (e.g. list(BIO_lifeHistory = "clutch_size > 4", MAP_meanAltitude = "meanAltitude < 1000", metadata_ranges = "Area < 1000"))

Any valid SQL expression can be used to build up a subset. See http://www.sqlite.org/lang_expr.html

When using cl parameter you must load the apropiated packages used in FUN by loading the packages inside the function or initializing the cluster before calling rangeMap.save (e.g. clusterEvalQ(cl=cl, library(caper)))).

See Also

metadata.update.

Examples

Run this code
require(rangeMapper)
require(rgdal)
breding_ranges = readOGR(system.file(package = "rangeMapper",
     "extdata", "wrens", "vector_combined"), "wrens", verbose = FALSE)
breding_ranges = spTransform(breding_ranges,
    CRS("+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0
        +ellps=WGS84 +units=m +no_defs") )
data(wrens)
d = subset(wrens, select = c('sci_name', 'body_size', 'body_mass', 'clutch_size') )

con = ramp("wrens.sqlite", gridSize = 200000, spdf = breding_ranges, biotab = d,
            ID = "sci_name", metadata = rangeTraits(),
            FUN = "median", overwrite = TRUE)


lmSlope = function(formula, data) {
    fm = try(lm(formula, data = data), silent = TRUE)
    if (inherits(fm, "try-error"))
        res = NA else res = coef(fm)[2]
    as.numeric(res)
}

# Subsetting by Species and Assembladge
rangeMap.save(con, FUN = lmSlope, biotab = "biotab", biotrait = "body_mass",
    tableName = "slope_bodyMass_clutchSize", formula = log(body_mass) ~ clutch_size,
    list(MAP_species_richness = "species_richness >= 5",
        BIO_biotab = "body_size > 15"
        ), overwrite = TRUE)

## Not run: 
# # Import raster maps to the current project
# r = system.file(package = "rangeMapper", "extdata", "etopo1", "etopo1_Americas.tif")
# rangeMap.save(con, path = r, tableName = "meanAltitude", FUN = mean, overwrite = TRUE)
# ## End(Not run)


m = rangeMap.fetch(con, spatial = FALSE)
plot(m)

Run the code above in your browser using DataLab