R/check_install_TMB.R

Defines functions check_install_TMB

Documented in check_install_TMB

#' Check TMB is working
#'
#' Check that the installation of TMB is working before trying to run any TMB
#' models.
#'
#' @details
#' This function will generate a lot of output to your screen that can be
#' helpful when trying to troubleshoot the installation of TMB. Most
#' importantly, you can see the location of where your computer is trying to
#' find make files. If you have already ran this function on your computer,
#' then you should see `make: Nothing to be done for 'all'`. If the
#' installation of TMB has failed, then you will see `"Error in
#' TMB::compile(...)"`. The most likely culprit for it failing is a faulty
#' installation of rtools, specifically rtools40 for R version 4.0 and 4.1 and
#' rtools42 for R version 4.20 and greater.
#'
#' @return A single integer is returned, where zero means that your
#' installation of rtools and TMB is good to go. I hope you get a zero! If the
#' status is failed, then you will see `"Compilation failed"`, a stop message
#' generated by [TMB::compile()].
#' @seealso
#' * `pkgbuild::find_rtools[]`
#' @export
#' @author Kelli F. Johnson
#' @examples
#' # Hope for a 0
#' \dontrun{
#' check_install_TMB()
#' }
#' # If you get something other than 0, run
#' pkgbuild::find_rtools()
#'
check_install_TMB <- function() {
  stopifnot(pkgbuild::find_rtools())
  packages <- utils::installed.packages()
  if (!"TMB" %in% packages[, "Package"]) {
    stop("Use the following code to install TMB\n",
      "install.packages('TMB')")
  }
  if (!"TMBhelper" %in% packages[, "Package"]) {
    stop("Use the following code to install TMBhelper\n",
      "remotes::install_github('kaskr/TMB_contrib_R/TMBhelper')")
  }

  wd <- getwd()
  on.exit(setwd(wd), add = TRUE)
  setwd(system.file("examples", package = "TMB"))
  test <- TMB::compile("simple.cpp")
  return(test)
}
nwfsc-assess/PEPtools documentation built on Nov. 7, 2023, 1:33 p.m.