Skip to content

Commit

Permalink
Add function check_setup
Browse files Browse the repository at this point in the history
  • Loading branch information
stellabelin committed Dec 10, 2024
1 parent 2d83d65 commit cadbfde
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SystemRequirements:
Python (>= 3.11.0)
Imports:
reticulate (>= 1.19),
cli,
utils
Suggests:
testthat,
Expand Down
67 changes: 67 additions & 0 deletions R/check_setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#' @title
#' Checks setup of Pharmpy/pharmr
#'
#' @description
#' Checks if everything is setup correctly. The following things are checked:
#' * If Python is installed and has correct version
#' * If Pharmpy is available
#' * If Pharmpy and pharmr version matches
#'
#' @export
check_setup <- function() {
cli::cli_h3("Checking Python setup")
py_okay <- check_python()
if (py_okay) {
cli::cli_h3("Checking Pharmpy and pharmr")
pharmpy_okay <- check_pharmpy()
}
}

check_python <- function() {
py <- reticulate::py_discover_config()
if (is.null(py)) {
print_status('fail', 'No Python found')
return(FALSE)
}
print_status('success', 'Python found:', additional_info=py$python)
py_version <- as.character(py$version)
if (utils::compareVersion(py_version, '3.11') == -1) {
print_status('fail', 'Version not supported:', additional_info=py_version)
return(FALSE)
}
print_status('success', 'Version supported:', additional_info=py_version)
return(TRUE)
}

check_pharmpy <- function() {
pharmpy_avail <- reticulate::py_module_available('pharmpy')
if (isFALSE(pharmpy_avail)) {
print_status('fail', 'Could not find Pharmpy installation')
return(FALSE)
}
pharmpy_version <- reticulate::py_to_r(pharmpy$`__version__`)
print_status('success', 'Pharmpy found:', additional_info=pharmpy_version)
pharmr_version <- as.character(packageVersion('pharmr'))
if (utils::compareVersion(pharmpy_version, pharmr_version) != 0) {
print_status('fail', 'pharmr version does not match:', additional_info=pharmr_version)
return(FALSE)
}
print_status('success', 'pharmr version matches:', additional_info=pharmr_version)
return(TRUE)
}

print_status <- function(type, msg, additional_info=NULL) {
if (type == 'success') {
cli::cli_alert_success(msg)
}
else if (type == 'fail') {
cli::cli_alert_danger(msg)
}
else {
stop('Unknown status type')
}
if (!is.null(additional_info)) {
cat(' ')
cli::cli_alert(additional_info)
}
}
16 changes: 16 additions & 0 deletions man/check_setup.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cadbfde

Please sign in to comment.