Skip to content

Commit

Permalink
use rlang::check_installed instead of ensure_installed
Browse files Browse the repository at this point in the history
  • Loading branch information
egillax committed Dec 17, 2024
1 parent fbea755 commit 4c2d18c
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 68 deletions.
3 changes: 1 addition & 2 deletions R/CyclopsSettings.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ setIterativeHardThresholding <- function(
threshold = 1e-06,
delta = 0
) {

ensure_installed("IterativeHardThresholding")
rlang::check_installed("IterativeHardThresholding")

if (K < 1)
stop("Invalid maximum number of predictors")
Expand Down
4 changes: 2 additions & 2 deletions R/DatabaseMigration.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ migrateDataModel <- function(connectionDetails, databaseSchema, tablePrefix = ""


getDataMigrator <- function(connectionDetails, databaseSchema, tablePrefix = "") {
ensure_installed("ResultModelManager")
rlang::check_installed("ResultModelManager")

ResultModelManager::DataMigrationManager$new(
connectionDetails = connectionDetails,
Expand All @@ -65,4 +65,4 @@ getDataMigrator <- function(connectionDetails, databaseSchema, tablePrefix = "")
migrationPath = "migrations",
packageName = utils::packageName()
)
}
}
2 changes: 1 addition & 1 deletion R/FeatureImportance.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pfi <- function(plpResult, population, plpData, repeats = 1,
savePlpData(plpData, file = plpDataLocation)

if(is.null(cores)){
ensure_installed('parallel')
rlang::check_installed("parallel")
ParallelLogger::logInfo(paste0('Number of cores not specified'))
cores <- parallel::detectCores()
ParallelLogger::logInfo(paste0('Using all ', cores))
Expand Down
2 changes: 1 addition & 1 deletion R/GradientBoostingMachine.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ setGradientBoostingMachine <- function(ntrees=c(100, 300), nthread=20, earlyStop
scalePosWeight=1, lambda=1, alpha=0,
seed= sample(10000000,1)){

ensure_installed("xgboost")
rlang::check_installed("xgboost")

checkIsClass(seed, c('numeric', 'integer'))

Expand Down
36 changes: 0 additions & 36 deletions R/HelperFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,6 @@ removeInvalidString <- function(string){
}


# Borrowed from devtools: https://github.com/hadley/devtools/blob/ba7a5a4abd8258c52cb156e7b26bb4bf47a79f0b/R/utils.r#L44
is_installed <- function (pkg) {
installed_version <- tryCatch(utils::packageVersion(pkg),
error = function(e) NA)
!is.na(installed_version)
}

# Borrowed and adapted from devtools: https://github.com/hadley/devtools/blob/ba7a5a4abd8258c52cb156e7b26bb4bf47a79f0b/R/utils.r#L74
ensure_installed <- function(pkg) {
if (!is_installed(pkg)) {
msg <- paste0(sQuote(pkg), " must be installed for this functionality.")
if (interactive()) {
message(msg, "\nWould you like to install it?")
if (utils::menu(c("Yes", "No")) == 1) {
if(pkg%in%c('BigKnn', "IterativeHardThresholding", "ShinyAppBuilder", "ResultModelManager")){

# add code to check for devtools...
dvtCheck <- tryCatch(utils::packageVersion('devtools'),
error = function(e) NA)
if(is.na(dvtCheck)){
utils::install.packages('devtools')
}

devtools::install_github(paste0('OHDSI/',pkg))
}else{
utils::install.packages(pkg)
}
} else {
stop(msg, call. = FALSE)
}
} else {
stop(msg, call. = FALSE)
}
}
}

#' Create a temporary model location
#'
#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/ImportFromCsv.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ insertCsvToDatabase <- function(
csvTableAppend = ''
){

ensure_installed('readr')
rlang::check_installed("readr")

ParallelLogger::logInfo('Starting input checks')

Expand Down
2 changes: 1 addition & 1 deletion R/KNN.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#' }
#' @export
setKNN <- function(k=1000, indexFolder=file.path(getwd(),'knn'), threads = 1 ){
ensure_installed("BigKnn")
rlang::check_installed("BigKnn")

checkIsClass(indexFolder, c('character'))

Expand Down
2 changes: 1 addition & 1 deletion R/LearningCurve.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ createLearningCurve <- function(
ExecutionDateTime <- Sys.time()

if(parallel){
ensure_installed('parallel')
rlang::check_installed("parallel")
if(is.null(cores)){
ParallelLogger::logInfo(paste0('Number of cores not specified'))
cores <- parallel::detectCores()
Expand Down
2 changes: 1 addition & 1 deletion R/LightGBM.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ setLightGBM <- function(nthread = 20,
scalePosWeight = 1,
isUnbalance = FALSE,
seed = sample(10000000, 1)) {
ensure_installed("lightgbm")
rlang::check_installed("lightgbm")
checkIsClass(seed, c("numeric", "integer"))

if (length(nthread) > 1) {
Expand Down
3 changes: 1 addition & 2 deletions R/Plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ outcomeSurvivalPlot <- function(
yLabel = 'Fraction of those who are outcome free in target population'
)
{

ensure_installed("survminer")
rlang::check_installed("survminer")
if(missing(plpData)){
stop('plpData missing')
}
Expand Down
6 changes: 3 additions & 3 deletions R/SaveLoadPlp.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ loadPlpModel <- function(dirPath) {


if(attr(plpModel, 'saveType') == "xgboost"){
ensure_installed("xgboost")
rlang::check_installed("xgboost")
plpModel$model <- xgboost::xgb.load(file.path(dirPath, "model.json"))
} else if(attr(plpModel, 'saveType') == "lightgbm"){
ensure_installed("lightgbm")
rlang::check_installed("lightgbm")
plpModel$model <- lightgbm::lgb.load(file.path(dirPath, "model.json"))
} else if(attr(plpModel, 'saveType') %in% c("RtoJson")){
plpModel$model <- ParallelLogger::loadSettingsFromJson(file.path(dirPath, "model.json"))
Expand Down Expand Up @@ -579,7 +579,7 @@ extractDatabaseToCsv <- function(
fileAppend = NULL
){

ensure_installed('readr')
rlang::check_installed("readr")

# check inputs
if(!is.null(fileAppend)){
Expand Down
7 changes: 3 additions & 4 deletions R/ViewShinyPlp.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ viewDatabaseResultPlp <- function(
# one shiny app

viewPlps <- function(databaseSettings){
ensure_installed("ShinyAppBuilder")
ensure_installed("ResultModelManager")

rlang::check_installed("ShinyAppBuilder")
rlang::check_installed("ResultModelManager")
connectionDetails <- do.call(
DatabaseConnector::createConnectionDetails,
databaseSettings$connectionDetailSettings
Expand Down Expand Up @@ -177,4 +176,4 @@ viewPlps <- function(databaseSettings){
}


}
}
6 changes: 2 additions & 4 deletions R/uploadToDatabase.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ insertRunPlpToSqlite <- function(

sqliteLocation <- tempdir()

ensure_installed('RSQLite')
rlang::check_installed("RSQLite")

# create sqlite database
connectionDetails <- DatabaseConnector::createConnectionDetails(
Expand Down Expand Up @@ -121,9 +121,7 @@ insertResultsToSqlite <- function(
if(!dir.exists(sqliteLocation)){
dir.create(sqliteLocation, recursive = T)
}

ensure_installed('RSQLite')

rlang::check_installed("RSQLite")
# create sqlite database
connectionDetails <- DatabaseConnector::createConnectionDetails(
dbms = 'sqlite',
Expand Down
9 changes: 0 additions & 9 deletions tests/testthat/test-helperfunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@
library("testthat")
context("HelperFunctions")

test_that("is_installed", {
testthat::expect_equal(PatientLevelPrediction:::is_installed('FeatureExtraction'), T)
testthat::expect_equal(PatientLevelPrediction:::is_installed('MadeUp4u834t3f'), F)
})

test_that("ensure_installed", {
testthat::expect_equal(PatientLevelPrediction:::ensure_installed('FeatureExtraction'), NULL)
})

# how to test checkPlpInstallation?


Expand Down

0 comments on commit 4c2d18c

Please sign in to comment.