diff --git a/NAMESPACE b/NAMESPACE index b62bc17..9b934db 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ importFrom(broom,augment) importFrom(broom,glance) importFrom(broom,tidy) importFrom(doParallel,registerDoParallel) +importFrom(doSNOW,registerDoSNOW) importFrom(dplyr,arrange) importFrom(dplyr,bind_rows) importFrom(dplyr,case_when) diff --git a/R/Extintions.R b/R/Extintions.R index 27b39ed..760f5c5 100644 --- a/R/Extintions.R +++ b/R/Extintions.R @@ -139,6 +139,7 @@ SimulateExtinctions <- function(Network, Method, Order = NULL, #' @param RewiringProb a numeric which identifies the threshold at which to assume rewiring potential is met. #' @param verbose Logical. Whether to report on function progress or not. #' @param RecalcConnect Logical or Numeric. Whether to recalculate connectedness of each node following each round of extinction simulation and subsequently update extinction order with newly mostconnected nodes. +#' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger. #' @return exports list containing a data frame with the characteristics of the network after every extinction and a network object containing the final network. The resulting data frame contains 11 columns that incorporate the topological index, the secondary extinctions, predation release, and total extinctions of the network in each primary extinction. #' @details When NetworkType = Trophic, secondary extinctions only occur for any predator, but not producers. If NetworkType = Mutualistic, secondary extinctions occur for all species in the network. #' @@ -570,7 +571,7 @@ ExtinctionOrder <- function(Network, Order, NetworkType = "Trophic", clust.metho #' @param RewiringDist a numeric matrix of NxN dimension (N... number of nodes in Network). Contains, for example, phylogenetic or functional trait distances between nodes in Network which are used by the Rewiring argument to calculate rewiring probabilities. If Rewiring == function(x){x}, this matrix is expected to contain probabilities of a connection being present between species-pairs. #' @param RewiringProb a numeric which identifies the threshold at which to assume rewiring potential is met. #' @param verbose Logical. Whether to report on function progress or not. -#' #' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger. +#' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger. #' @return exports list containing a data frame with the characteristics of the network after every extinction, a network object containing the final network, and a graph with the mean and 95percent interval. The resulting data frame contains 11 columns that incorporate the topological index, the secondary extinctions, predation release, and total extinctions of the network in each primary extinction. #' @details When NetworkType = Trophic, secondary extinctions only occur for any predator, but not producers. If NetworkType = Mutualistic, secondary extinctions occur for all species in the network. #' @@ -624,6 +625,7 @@ ExtinctionOrder <- function(Network, Order, NetworkType = "Trophic", clust.metho #' @importFrom utils txtProgressBar #' @importFrom patchwork wrap_plots #' @importFrom patchwork plot_annotation +#' @importFrom doSNOW registerDoSNOW #' @author Derek Corcoran @@ -637,7 +639,7 @@ RandomExtinctions <- function(Network, nsim = 10, parallel = FALSE, ncores, IS = 0, Rewiring = FALSE, RewiringDist = NULL, RewiringProb = 0.5, - verbose = TRUE){ + verbose = TRUE, forceFULL = FALSE){ if(!NetworkType %in% c("Trophic", "Mutualistic")){stop("Please specify NetworkType as either 'Trophic' or 'Mutualistic'")} ## setting up objects NumExt <- sd <- AccSecExt <- AccSecExt_95CI <- AccSecExt_mean <- Lower <- Upper <-NULL @@ -649,13 +651,18 @@ RandomExtinctions <- function(Network, nsim = 10, ## simulations if(verbose & !parallel){ProgBar <- txtProgressBar(max = nsim, style = 3)} if(parallel){ - cl <- makeCluster(ncores, outfile="test.txt") - registerDoParallel(cl) + cl <- makeCluster(ncores) + # registerDoParallel(cl) + registerDoSNOW(cl) parallel::clusterExport(cl, - varlist = c("network", "SimNum", "IS", "Rewiring", "RewiringDist", "RewiringProb", "SimNum"), + varlist = c("network", "SimNum", "IS", "Rewiring", "RewiringDist", "RewiringProb", "SimNum", "forceFULL"), envir = environment() ) - sims <- foreach(i=1:nsim, .packages = c("NetworkExtinction", "dplyr"))%dopar%{ + pb <- txtProgressBar(max = nsim, style = 3) + progress <- function(i) setTxtProgressBar(pb, i) + opts <- list(progress = progress) + + sims <- foreach(i=1:nsim, .options.snow = opts, .packages = c("NetworkExtinction", "dplyr"))%dopar%{ Order <- sample(1:network::network.size(network), size = SimNum) sims1 <- try(ExtinctionOrder(Network = network, Order = Order, @@ -663,7 +670,7 @@ RandomExtinctions <- function(Network, nsim = 10, Rewiring = Rewiring, RewiringDist = RewiringDist, verbose = FALSE, RewiringProb = RewiringProb), silent = TRUE) try({sims1$sims$simulation <- i}, silent = TRUE) - sims1 + return(sims1) } stopCluster(cl) }else{ diff --git a/man/ExtinctionOrder.Rd b/man/ExtinctionOrder.Rd index 184437a..2466c2a 100644 --- a/man/ExtinctionOrder.Rd +++ b/man/ExtinctionOrder.Rd @@ -39,6 +39,8 @@ cluster_label_prop or cluster_infomap, defaults to cluster_infomap} \item{verbose}{Logical. Whether to report on function progress or not.} \item{RecalcConnect}{Logical or Numeric. Whether to recalculate connectedness of each node following each round of extinction simulation and subsequently update extinction order with newly mostconnected nodes.} + +\item{forceFULL}{Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger.} } \value{ exports list containing a data frame with the characteristics of the network after every extinction and a network object containing the final network. The resulting data frame contains 11 columns that incorporate the topological index, the secondary extinctions, predation release, and total extinctions of the network in each primary extinction. diff --git a/man/RandomExtinctions.Rd b/man/RandomExtinctions.Rd index e53264d..cd9132f 100644 --- a/man/RandomExtinctions.Rd +++ b/man/RandomExtinctions.Rd @@ -18,7 +18,8 @@ RandomExtinctions( Rewiring = FALSE, RewiringDist = NULL, RewiringProb = 0.5, - verbose = TRUE + verbose = TRUE, + forceFULL = FALSE ) } \arguments{ @@ -52,8 +53,9 @@ sequentially} \item{RewiringProb}{a numeric which identifies the threshold at which to assume rewiring potential is met.} -\item{verbose}{Logical. Whether to report on function progress or not. -#' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger.} +\item{verbose}{Logical. Whether to report on function progress or not.} + +\item{forceFULL}{Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger.} } \value{ exports list containing a data frame with the characteristics of the network after every extinction, a network object containing the final network, and a graph with the mean and 95percent interval. The resulting data frame contains 11 columns that incorporate the topological index, the secondary extinctions, predation release, and total extinctions of the network in each primary extinction.