-
Notifications
You must be signed in to change notification settings - Fork 1
/
stan_cmdstan_exists.R
32 lines (30 loc) · 1 KB
/
stan_cmdstan_exists.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#' @title Check if `instantiate` can find CmdStanR and CmdStan.
#' @export
#' @family cmdstan
#' @description Check if `instantiate` can find CmdStanR and CmdStan.
#' @inheritParams stan_cmdstan_path
#' @return `TRUE` if `instantiate` can find the CmdStanR R package and the
#' CmdStan command line tool. Returns `FALSE` otherwise.
#' @examples
#' stan_cmdstan_exists()
stan_cmdstan_exists <- function(
cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = "")
) {
# nocov start
if (!rlang::is_installed("cmdstanr")) {
return(FALSE) # Not possible to cover in regular unit tests.
}
# nocov end
path <- stan_cmdstan_path(cmdstan_install = cmdstan_install)
cmdstan_valid(path)
}
cmdstan_valid <- function(path) {
length(path) > 0L &&
nzchar(path) &&
dir.exists(path) &&
file.exists(file.path(path, "makefile")) &&
makefile_version_exists(file.path(path, "makefile"))
}
makefile_version_exists <- function(makefile) {
any(grepl(pattern = "^CMDSTAN_VERSION", x = readLines(makefile)))
}