Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automating dependency handling, platform interop, R CMD check housekeeping, etc #319

Merged
merged 13 commits into from
Oct 10, 2018
Merged
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
^\.Rproj\.user$
^.travis.yml$
deploy.sh
^\.github$
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inst/doc/* linguist-documentation
vignettes/* linguist-documentation
man/* linguist-documentation
extras/* linguist-documentation
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
Package: Achilles
Type: Package
Title: Creates descriptive statistics summary for an entire OMOP CDM instance
Title: Creates Descriptive Statistics Summary for an Entire OMOP CDM Instance
Version: 1.6
Date: 2018-04-09
Author: Patrick Ryan, Martijn Schuemie, Vojtech Huser, Chris Knoll, Ajit Londhe
Maintainer: Patrick Ryan <ryan@ohdsi.org>
LazyData: true
Description: creates descriptive statistics summary for an entire OMOP CDM
Description: Creates descriptive statistics summary for an entire OMOP CDM
instance. Since Aug 2016 only CDM v5 is actively being extended. Achilles
Heel component does data quality assesment.
Depends:
SqlRender,
DatabaseConnector (>= 2.0.0),
rjson,
OhdsiRTools
rjson
Suggests:
testthat,
dplyr,
R.utils
R.utils,
OhdsiRTools
Remotes:
OHDSI/OhdsiRTools
License: Apache License
Roxygen: list(wrap = FALSE)
RoxygenNote: 6.0.1
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ export(fetchAchillesHeelResults)
export(getAnalysisDetails)
export(showReportTypes)
export(validateSchema)
import(DatabaseConnector)
import(OhdsiRTools)
import(SqlRender)
import(rjson)
importFrom(utils,compareVersion)
importFrom(utils,packageVersion)
importFrom(utils,read.csv)
importFrom(utils,zip)
23 changes: 5 additions & 18 deletions R/Achilles-package.R
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
#' Achilles
#'
#' @name Achilles
#' @docType package
NULL




#' allReports
#'
#' A list of reports
#'
#'
#' @docType data
#' @keywords datasets
#' @name allReports

#' @import DatabaseConnector
#' @import OhdsiRTools
#' @import SqlRender
#' @import rjson
#' @importFrom utils compareVersion packageVersion read.csv zip
NULL
120 changes: 64 additions & 56 deletions R/Achilles.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -74,28 +74,28 @@
#' numThreads = 10)
#' }
#' @export
achilles <- function (connectionDetails,
cdmDatabaseSchema,
oracleTempSchema = cdmDatabaseSchema,
resultsDatabaseSchema = cdmDatabaseSchema,
scratchDatabaseSchema = resultsDatabaseSchema,
vocabDatabaseSchema = cdmDatabaseSchema,
sourceName = "",
analysisIds,
createTable = TRUE,
smallCellCount = 5,
cdmVersion = "5",
runHeel = TRUE,
validateSchema = FALSE,
runCostAnalysis = FALSE,
conceptHierarchy = TRUE,
createIndices = TRUE,
numThreads = 1,
tempAchillesPrefix = "tmpach",
dropScratchTables = TRUE,
sqlOnly = FALSE,
outputFolder = "output",
logMultiThreadPerformance = FALSE) {
achilles <- function(connectionDetails,
cdmDatabaseSchema,
# oracleTempSchema = cdmDatabaseSchema, # Unused arg
resultsDatabaseSchema = cdmDatabaseSchema,
scratchDatabaseSchema = resultsDatabaseSchema,
vocabDatabaseSchema = cdmDatabaseSchema,
sourceName = "",
analysisIds,
createTable = TRUE,
smallCellCount = 5,
cdmVersion = "5",
runHeel = TRUE,
validateSchema = FALSE,
runCostAnalysis = FALSE,
conceptHierarchy = TRUE,
createIndices = TRUE,
numThreads = 1,
tempAchillesPrefix = "tmpach",
dropScratchTables = TRUE,
sqlOnly = FALSE,
outputFolder = "output",
logMultiThreadPerformance = FALSE) {

achillesSql <- c()

Expand Down Expand Up @@ -188,17 +188,27 @@ achilles <- function (connectionDetails,
schemaDelim <- "."

if (numThreads == 1 || scratchDatabaseSchema == "#") {
message("Beginning single-threaded operations")

numThreads <- 1
scratchDatabaseSchema <- "#"
schemaDelim <- "s_"

# first invocation of the connection, to persist throughout to maintain temp tables
connection <- DatabaseConnector::connect(connectionDetails = connectionDetails)
} else if (!requireNamespace("OhdsiRTools", quietly = TRUE)) {
stop(
"Multi-threading support requires package 'OhdsiRTools'.",
" Consider running single-threaded by setting",
" `numThreads = 1` and `scratchDatabaseSchema = '#'`.",
" You may install it using devtools with the following code:",
"\n devtools::install_github('OHDSI/OhdsiRTools')",
"\n\nAlternately, you might want to install ALL suggested packages using:",
"\n devtools::install_github('OHDSI/Achilles', dependencies = TRUE)",
call. = FALSE
)
} else {
if (!.is_installed("OhdsiRTools")) {
writeLines("Installing OhdsiRTools for multi-threading support")
devtools::install_github("OHDSI/OhdsiRTools")
}
message("Beginning multi-threaded operations")
}

# Create analysis table -------------------------------------------------------------
Expand Down Expand Up @@ -411,15 +421,15 @@ achilles <- function (connectionDetails,

if (numThreads > 1 & !sqlOnly) {
# Drop the scratch tables
writeLines(sprintf("Dropping scratch Achilles tables from schema %s", scratchDatabaseSchema))
message(sprintf("Dropping scratch Achilles tables from schema %s", scratchDatabaseSchema))

dropAllScratchTables(connectionDetails = connectionDetails,
scratchDatabaseSchema = scratchDatabaseSchema,
tempAchillesPrefix = tempAchillesPrefix,
numThreads = numThreads,
tableTypes = c("achilles", "concept_hierarchy"))

writeLines(sprintf("Temporary Achilles tables removed from schema %s", scratchDatabaseSchema))
message(sprintf("Temporary Achilles tables removed from schema %s", scratchDatabaseSchema))
}

# Generate Main Analyses ----------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -448,7 +458,7 @@ achilles <- function (connectionDetails,
achillesSql <- c(achillesSql, lapply(mainSqls, function(s) s$sql))

if (!sqlOnly) {
writeLines("Executing multiple queries. This could take a while")
message("Executing multiple queries. This could take a while")

if (numThreads == 1) {
for (mainSql in mainSqls) {
Expand Down Expand Up @@ -501,7 +511,7 @@ achilles <- function (connectionDetails,

if (!sqlOnly) {

writeLines("Merging scratch Achilles tables")
message("Merging scratch Achilles tables")

if (numThreads == 1) {
for (sql in mergeSqls) {
Expand All @@ -521,7 +531,7 @@ achilles <- function (connectionDetails,
}

if (!sqlOnly) {
writeLines(sprintf("Done. Achilles results can now be found in schema %s", resultsDatabaseSchema))
message(sprintf("Done. Achilles results can now be found in schema %s", resultsDatabaseSchema))
}

# Clean up scratch tables -----------------------------------------------
Expand All @@ -531,15 +541,15 @@ achilles <- function (connectionDetails,
DatabaseConnector::disconnect(connection = connection)
} else if (dropScratchTables & !sqlOnly) {
# Drop the scratch tables
writeLines(sprintf("Dropping scratch Achilles tables from schema %s", scratchDatabaseSchema))
message(sprintf("Dropping scratch Achilles tables from schema %s", scratchDatabaseSchema))

dropAllScratchTables(connectionDetails = connectionDetails,
scratchDatabaseSchema = scratchDatabaseSchema,
tempAchillesPrefix = tempAchillesPrefix,
numThreads = numThreads,
tableTypes = c("achilles"))

writeLines(sprintf("Temporary Achilles tables removed from schema %s", scratchDatabaseSchema))
message(sprintf("Temporary Achilles tables removed from schema %s", scratchDatabaseSchema))
}

# Create concept hierarchy table -----------------------------------------------------------------
Expand Down Expand Up @@ -603,10 +613,10 @@ achilles <- function (connectionDetails,

if (sqlOnly) {
SqlRender::writeSql(sql = paste(achillesSql, collapse = "\n\n"), targetFile = file.path(outputFolder, "achilles.sql"))
writeLines(sprintf("All Achilles SQL scripts can be found in folder: %s", file.path(outputFolder, "achilles.sql")))
message(sprintf("All Achilles SQL scripts can be found in folder: %s", file.path(outputFolder, "achilles.sql")))
}

return (achillesResults)
achillesResults
}

#' Create the concept hierarchy
Expand Down Expand Up @@ -676,7 +686,7 @@ createConceptHierarchy <- function(connectionDetails,


if (!sqlOnly) {
writeLines("Executing Concept Hierarchy creation. This could take a while")
message("Executing Concept Hierarchy creation. This could take a while")

if (numThreads == 1) {
connection <- DatabaseConnector::connect(connectionDetails = connectionDetails)
Expand Down Expand Up @@ -707,10 +717,10 @@ createConceptHierarchy <- function(connectionDetails,
numThreads = numThreads,
tableTypes = c("concept_hierarchy"))

writeLines(sprintf("Done. Concept Hierarchy table can now be found in %s", resultsDatabaseSchema))
message(sprintf("Done. Concept Hierarchy table can now be found in %s", resultsDatabaseSchema))
}

return (c(hierarchySqls, mergeSql))
c(hierarchySqls, mergeSql)
}


Expand Down Expand Up @@ -744,7 +754,7 @@ createIndices <- function(connectionDetails,
DatabaseConnector::disconnect(connection = connection)
}

return (indicesSql)
indicesSql
}


Expand Down Expand Up @@ -795,11 +805,11 @@ validateSchema <- function(connectionDetails,
} else {
connection <- DatabaseConnector::connect(connectionDetails = connectionDetails)
tables <- DatabaseConnector::querySql(connection = connection, sql = sql)
writeLines("CDM Schema is valid")
message("CDM Schema is valid")
DatabaseConnector::disconnect(connection = connection)
}

return (sql)
sql
}

#' Get all analysis details
Expand All @@ -812,9 +822,14 @@ validateSchema <- function(connectionDetails,
#'
#' @export
getAnalysisDetails <- function() {
pathToCsv <- system.file("csv", "achilles", "achilles_analysis_details.csv", package = "Achilles")
analysisDetails <- read.csv(file = pathToCsv, header = TRUE, stringsAsFactors = FALSE)
return (analysisDetails)
read.csv( # Recommend saving as an rda file in ./data/
system.file(
"csv",
"achilles",
"achilles_analysis_details.csv",
package = "Achilles"),
stringsAsFactors = FALSE
)
}

#' Drop all possible scratch tables
Expand Down Expand Up @@ -951,15 +966,14 @@ dropAllScratchTables <- function(connectionDetails,
cdmDatabaseSchema = cdmDatabaseSchema)$sql
connection <- DatabaseConnector::connect(connectionDetails = connectionDetails)
cdmVersion <- tryCatch({
c <- DatabaseConnector::querySql(connection = connection, sql = sql)
}, error = function (e) {
c <- ""
DatabaseConnector::querySql(connection = connection, sql = sql)
}, error = function(e) {
""
}, finally = {
DatabaseConnector::disconnect(connection = connection)
connection <- NULL
})

return (c)
cdmVersion
}

.getAnalysisSql <- function(analysisId,
Expand Down Expand Up @@ -1047,9 +1061,3 @@ dropAllScratchTables <- function(connectionDetails,

saveRDS(object = newDf, file = logFile)
}

.is_installed <- function(pkg, version = 0) {
installed_version <- tryCatch(utils::packageVersion(pkg), error = function(e) NA)
!is.na(installed_version) && installed_version >= version
}

Loading