Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to Jags detection error #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ error_reproduction_scripts

# To reduce repo size
vignettes/vignette_files
vignettes/vignette_cache
vignettes/vignette_cache
inst/doc/BRCindicators_files
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ Imports:
RColorBrewer,
reshape2,
jagsUI,
runjags
rjags
Suggests:
covr,
knitr,
rmarkdown,
snowfall,
sparta,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
runjags
VignetteBuilder:
knitr
Config/testthat/edition: 3
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ importFrom(car,logit)
importFrom(coda,as.mcmc)
importFrom(coda,mcmc.list)
importFrom(reshape2,dcast)
importFrom(runjags,testjags)
25 changes: 15 additions & 10 deletions R/bayesian_meta_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#' @param save.sppars Logical. Should the species-specific parameters be monitored? Defaults to TRUE
#' @param CI defines the credible intervals of the posterior distribution to report. Defaults the 95th percentile
#' @param seed Option to set a custom seed to initialize JAGS chains, for reproducibility. Should be an integer. This argument will be deprecated in the next version, but you can always set the outside the function yourself.
#' @param jags_path The full path to the JAGS executable. The default value is null. If null, will search the default installation path of the operating system. If specified, the path will be set to the JAGS_HOME system variable.
#' @details There are a number of model to choose from:
#' \itemize{
#' \item{\code{"smooth"}}{ The default option. Indicator defined by Growth rates, with Ruppert smoother, allowing for species to join late. Error on the first year of each species' time-series is assumed to be zero. The indicator is the expected value of the geometric mean across species (with missing species imputed).
Expand All @@ -42,11 +43,9 @@
#' A generic method for estimating and smoothing multispecies biodiversity indices, robust to intermittent data.
#' \emph{Journal of Agricultural Biological and Environmental Statistics}, in revision.
#' @export
#' @examples
#'
#' # Only run if there is a JAGS installation
#' if(suppressWarnings(runjags::testjags(silent = TRUE)$JAGS.found)){
#'
#' @examples
#' \dontrun{
#' # Create some example data in the format required
#' data <- data.frame(species = rep(letters, each = 50),
#' year = rep(1:50, length(letters)),
Expand All @@ -59,8 +58,7 @@
#' # Plot the resulting indicator
#' plot_indicator(indicator = bma_indicator[,'Index.Mprime'],
#' CIs = bma_indicator[,c(3,4)])
#'
#' }
#' }

bma <- function (data,
plot = TRUE,
Expand All @@ -81,14 +79,21 @@ bma <- function (data,
save.sppars = TRUE,
incl.2deriv = FALSE,
CI = 95,
seed = NULL){
seed = NULL,
jags_path = NULL){

set.seed(seed = seed)

# Check if JAGS is installed
if (!detect_jags()) {
stop("No installation of JAGS has been detected. You can install JAGS from https://sourceforge.net/projects/mcmc-jags/files/JAGS/",
call. = FALSE)
if (!detect_jags(jags_path)) {
stop(
"JAGS could not be found\n",
"If you have not installed JAGS, please install it from: https://sourceforge.net/projects/mcmc-jags/\n",
"If JAGS has been installed to the default location, restart the console, and this error should not re-occur. If it does, specify the full path to the JAGS executable",
"To quickly recover the path of the jags executable you can run the following:\n",
"library(rjags)\n",
"runjags::findjags()"
)
}

# Check to see that column names are present
Expand Down
53 changes: 48 additions & 5 deletions R/detect_jags.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
# Internal function to detect JAGS installation.
#' @importFrom runjags testjags
#'
detect_jags <- function(){
return(suppressWarnings(runjags::testjags(silent = TRUE)$JAGS.found))
}
#'
detect_jags <- function(jags_path){

# Get the operating system type
os_type <- Sys.info()["sysname"]

if(!is.null(jags_path)){

message("A non-default jags installation path has been specified. Setting jags path as an environment variable")

# Set JAGS_HOME variable
Sys.setenv(JAGS_HOME = jags_path)

return(TRUE)

} else {

if (os_type == "Windows") {

# Search the default installation path for Windows
user_home <- Sys.getenv("USERPROFILE")
jags_dir <- file.path(user_home, "AppData", "Local", "Programs", "JAGS")

jags_path <- list.files(jags_dir, full.name = TRUE, recursive = TRUE, pattern = "*jags-terminal.exe")

} else if (os_type == "Darwin") {

# Search the default installation path for macOS
jags_path <- list.files("/usr/local/bin/JAGS", full.name = TRUE, recursive = TRUE, pattern = "*jags-terminal.exe")

} else {

message("Unsupported OS type, returning false for JAGS installation. To bypass, please specify the path to the jags executable in jags_path")
return(FALSE)

}

if(length(jags_path) > 0){

return(TRUE)

} else {

return(FALSE)

}
}
}
Loading
Loading