process_input <- function(run_dat){
run_dat$inits <- process_inits(run_dat$inits, run_dat$n_chains,
run_dat$n_cores)
run_dat
}
#------------------------------------------------------------------------------
#Check and clean up inits
process_inits <- function(raw_inits, n_chains, n_cores){
all_list <- function(l) all(sapply(l,class)=='list')
if(is.null(raw_inits)){
if(n_cores > 1){
#Make blank list of lists to add RNG to later
inits <- replicate(n_chains,list())
} else {
#Do nothing (let JAGS handle inits)
return(NULL)
}
} else if(is.function(raw_inits)){
#If function provided, run it to generate values
inits <- replicate(n_chains,raw_inits(),simplify=F)
#Check to make sure the result is a list of lists
if(!all_list(inits)){
stop('Inits function must return a list')
}
} else if(is.list(raw_inits)){
#If list provided, check it is the right length
if(length(raw_inits)!=n_chains){
stop('length(inits) != number of chains')
} else if(!all_list(raw_inits)){
#Check all elements are lists
stop('All elements of inits must be lists')
} else {
inits <- raw_inits
}
} else {
stop('If provided, inits must be a function or a list of lists')
}
#Add RNG to inits if running in parallel
if(n_cores > 1) inits <- add_RNG(inits)
inits
}
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#Add RNG seed to inits list
add_RNG <- function(inits_list){
#Don't replace existing RNG seed if it exists
if(".RNG.seed" %in% names(inits_list[[1]])) return(inits_list)
for (i in 1:length(inits_list)){
inits_list[[i]]$.RNG.name="base::Mersenne-Twister"
inits_list[[i]]$.RNG.seed=sample(1:1e5,1)
}
inits_list
}
#------------------------------------------------------------------------------
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.