From cadbfdecc28a07f08e10c5fb4c2e4dd77c051bc2 Mon Sep 17 00:00:00 2001 From: Stella Date: Tue, 10 Dec 2024 13:42:19 +0100 Subject: [PATCH] Add function check_setup --- DESCRIPTION | 1 + R/check_setup.R | 67 ++++++++++++++++++++++++++++++++++++++++++++++ man/check_setup.Rd | 16 +++++++++++ 3 files changed, 84 insertions(+) create mode 100644 R/check_setup.R create mode 100644 man/check_setup.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 89ffd6a..ef188b8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,6 +24,7 @@ SystemRequirements: Python (>= 3.11.0) Imports: reticulate (>= 1.19), + cli, utils Suggests: testthat, diff --git a/R/check_setup.R b/R/check_setup.R new file mode 100644 index 0000000..4d08bad --- /dev/null +++ b/R/check_setup.R @@ -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) + } +} diff --git a/man/check_setup.Rd b/man/check_setup.Rd new file mode 100644 index 0000000..595af7d --- /dev/null +++ b/man/check_setup.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/check_setup.R +\name{check_setup} +\alias{check_setup} +\title{Checks setup of Pharmpy/pharmr} +\usage{ +check_setup() +} +\description{ +Checks if everything is setup correctly. The following things are checked: +\itemize{ +\item If Python is installed and has correct version +\item If Pharmpy is available +\item If Pharmpy and pharmr version matches +} +}