diff --git a/R/FUN.TRY.R b/R/FUN.TRY.R index cbe4bb59eb1fe78510e543edef9201132b8d3d46..1231667983f8c0f2cdb747b8232ccebe5c48054e 100644 --- a/R/FUN.TRY.R +++ b/R/FUN.TRY.R @@ -1,9 +1,9 @@ ############################################ FUNCTION TO EXTRACT DECTED OUTLIER AND FORMAT TRY DATA Georges Kunstler ############################################ 14/06/2013 -library(MASS) -library(doParallel) -library(mvoutlier) +library(MASS, quietly=TRUE) +library(doParallel, quietly=TRUE) +library(mvoutlier, quietly=TRUE) ######################################################## Build a function that extract the variables @@ -133,7 +133,7 @@ f.quantile <- function(x, ind, probs) { } f.quantile.boot2 <- function(x, R, probs = 0.99) { - require(boot) + require(boot, quietly=TRUE) if (length(na.exclude(x)) > 0) { quant.boot <- boot(x, f.quantile, R = R, probs = probs) return(c(mean = mean(quant.boot$t), sd = sd(quant.boot$t), nobs = length(na.exclude(x)))) diff --git a/R/format.function.R b/R/format.function.R index b5c4288ed9e3abcb4fba85bf42414063cc6d7820..f7657876bee6f8a9f42c486a2b7aaeb3e6aa79b1 100644 --- a/R/format.function.R +++ b/R/format.function.R @@ -56,7 +56,7 @@ BA.fun <- function(diam, weights) { ##' @return data frame with tree.id and one column per species with basal area of the species (without the target tree) ##' @author Kunstler BA.SP.FUN <- function(obs.id, diam, sp, id.plot, weights, weight.full.plot) { - require(data.table) + require(data.table, quietly=TRUE) id.plot <- as.character(id.plot) obs.id <- as.character(obs.id) @@ -133,7 +133,7 @@ BA.SP.FUN <- function(obs.id, diam, sp, id.plot, weights, weight.full.plot) { BA.SP.FUN.XY <- function(obs.id, xy.table, diam, sp, Rlim, parallel = FALSE, rpuDist = FALSE) { rownames(xy.table) <- obs.id if (rpuDist) { - require(rpud) + require(rpud, quietly=TRUE) dist.mat <- rpuDist(xy.table, upper = TRUE, diag = TRUE) } else { dist.mat <- as.matrix(dist(xy.table, upper = TRUE, diag = TRUE)) @@ -149,7 +149,7 @@ BA.SP.FUN.XY <- function(obs.id, xy.table, diam, sp, Rlim, parallel = FALSE, rpu fun.sum.sp <- function(x, sp) tapply(x, INDEX = sp, FUN = sum, na.rm = TRUE) if (parallel) { ## parallel version - require(doParallel) + require(doParallel, quietly=TRUE) registerDoParallel(cores = 4) mat <- dist.mat * BA.mat res.temp <- foreach(i = 1:nrow(mat), .combine = rbind) %dopar% { @@ -185,14 +185,14 @@ fun.clean.species.tab <- function(species.tab) { ### compute quantile 99% and sd with a bootstrap -library(boot) +library(boot, quietly=TRUE) f.quantile <- function(x, ind, probs) { quantile(x[ind], probs = probs, na.rm = TRUE) } f.quantile.boot <- function(i, x, fac, R, probs = 0.99) { - require(boot) + require(boot, quietly=TRUE) if (length(na.exclude(x[fac == i])) > 0) { quant.boot <- boot(x[fac == i], f.quantile, R = R, probs = probs) return(as.matrix(c(mean = mean(quant.boot$t), sd = sd(quant.boot$t), nobs = length(na.exclude(x[fac == @@ -231,11 +231,11 @@ function.replace.NA.negative <- function(data.BA.SP) { ############################################################## function to generate data in good format per ecoregion fun.data.per.ecoregion <- function(ecoregion, data.tot, plot.name, weight.full.plot, - name.country, data.TRY = NA, species.lookup = NA) { - require(data.table) - data.tot <- data.table(data.tot) - data <- data.tot[ecocode == ecoregion, ] - rm(data.tot) + name.country, data.TRY = NA, species.lookup = NA, output.dir = "./data/process") { + require(data.table, quietly=TRUE) + + print(paste("Working on Ecoregion %s", ecoregion)) + data <- data.table(data.tot)[ecocode == ecoregion, ] data.BA.SP <- BA.SP.FUN(obs.id = as.vector(data[["obs.id"]]), diam = as.vector(data[["D"]]), sp = as.vector(data[["sp"]]), id.plot = as.vector(data[[plot.name]]), weights = data[["weights"]], weight.full.plot = weight.full.plot) @@ -264,20 +264,17 @@ fun.data.per.ecoregion <- function(ecoregion, data.tot, plot.name, weight.full.p if (sum(!data.BA.sp[["obs.id"]] == data[["obs.id"]]) > 0) stop("competition index not in the same order than data") ##### ADD TRY DATA OR TRAITS IF NEEDED + data.traits = NA if (!is.na(data.TRY)) { - sp.extract <- species.lookup[species.lookup[["sp"]] %in% unique(data[["sp"]]), - ] + sp.extract <- species.lookup[species.lookup[["sp"]] %in% unique(data[["sp"]]), ] data.traits <- fun.extract.format.sp.traits.TRY(sp = sp.extract[["sp"]], sp.syno.table = sp.extract, data.TRY) - ## save everything as a list - list.temp <- list(data.tree = data, data.BA.SP = data.BA.sp, data.traits = data.traits) - save(list.temp, file = paste("./data/process/list", name.country, ecoregion, - "Rdata", sep = ".")) - } else { - list.temp <- list(data.tree = data, data.BA.SP = data.BA.sp, data.traits = NA) - save(list.temp, file = paste("./data/process/list", name.country, ecoregion, - "Rdata", sep = ".")) - } + } + + ## save everything as a list + list.temp <- list(data.tree = data, data.BA.SP = data.BA.sp, data.traits = data.traits) + save(list.temp, file = file.path(output.dir,paste("list", name.country, ecoregion, + "Rdata", sep = "."))) }