diff --git a/.gitignore b/.gitignore index 27a3b7e2..bbcd4543 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .Rproj.user inst/doc .DS_Store +errorReportSql.txt +*.log diff --git a/NAMESPACE b/NAMESPACE index 553b9cc3..466239bb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -73,6 +73,7 @@ export(createVisitOccurrence) export(createVisitSourceConceptAttribute) export(createVisitTypeExcludeAttribute) export(createWindow) +export(getConceptIdDetails) export(getConceptSetExpression) export(getConceptSetId) export(listAttributeOptions) @@ -86,6 +87,8 @@ export(readInCirce) export(saveComponent) export(toggleConceptMapping) export(writeCaprCall) +import(DatabaseConnector) +import(dplyr) import(methods) importFrom(CirceR,buildCohortQuery) importFrom(CirceR,cohortExpressionFromJson) @@ -107,6 +110,7 @@ importFrom(purrr,map2) importFrom(purrr,map_int) importFrom(rlang,"!!!") importFrom(rlang,"!!") +importFrom(rlang,.data) importFrom(rlang,call2) importFrom(rlang,expr) importFrom(rlang,sym) diff --git a/R/userConceptLookupFn.R b/R/userConceptLookupFn.R index 37582172..dcc585c1 100644 --- a/R/userConceptLookupFn.R +++ b/R/userConceptLookupFn.R @@ -14,7 +14,55 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# + + + +#' get concept id details +#' +#' For one or more concept id, get concept id details by querying the +#' OMOP vocabulary in the database. +#' +#' @template Connection +#' @template VocabularyDatabaseSchema +#' @template OracleTempSchema +#' @param conceptIds a vector of concept ids +#' @return a tibble data frame object with conceptId, conceptName, standardConcept, +#' standardConceptCaption, invalidReason, invalidReasonCaption, conceptCode, +#' domainId, vocabularyId, conceptClassId. +#' +#' @export +getConceptIdDetails <- function(conceptIds, + connectionDetails = NULL, + connection = NULL, + vocabularyDatabaseSchema = NULL, + oracleTempSchema = NULL) { + + errorMessage <- checkmate::makeAssertCollection() + checkmate::assertVector(conceptIds, add = errorMessage) + checkmate::reportAssertions(collection = errorMessage) + + # Set up connection to server ---------------------------------------------------- + if (is.null(connection)) { + if (!is.null(connectionDetails)) { + connection <- DatabaseConnector::connect(connectionDetails) + on.exit(DatabaseConnector::disconnect(connection)) + } else { + stop("No connection or connectionDetails provided.") + } + } + + conceptQuery <- "SELECT * FROM @vocabularyDatabaseSchema.concept WHERE concept_id IN (@conceptId);" + conceptDetails <- DatabaseConnector::renderTranslateQuerySql(connection = connection, + sql = conceptQuery, + snakeCaseToCamelCase = TRUE, + vocabularyDatabaseSchema = vocabularyDatabaseSchema, + oracleTempSchema = oracleTempSchema, + conceptId = conceptIds) %>% + dplyr::tibble() + return(conceptDetails) +} + + ##' Lookup Concepts by OMOP Concept Id #' #' This function looks up concepts using the OMOP concept id. Function requires a dbms connection to use diff --git a/man-roxygen/Connection.R b/man-roxygen/Connection.R new file mode 100644 index 00000000..fa593f5c --- /dev/null +++ b/man-roxygen/Connection.R @@ -0,0 +1,9 @@ +#' @param connectionDetails An object of type \code{connectionDetails} as created using the +#' \code{\link[DatabaseConnector]{createConnectionDetails}} function in the +#' DatabaseConnector package. Can be left NULL if \code{connection} is +#' provided. +#' @param connection An object of type \code{connection} as created using the +#' \code{\link[DatabaseConnector]{connect}} function in the +#' DatabaseConnector package. Can be left NULL if \code{connectionDetails} +#' is provided, in which case a new connection will be opened at the start +#' of the function, and closed when the function finishes. diff --git a/man-roxygen/OracleTempSchema.R b/man-roxygen/OracleTempSchema.R new file mode 100644 index 00000000..79675145 --- /dev/null +++ b/man-roxygen/OracleTempSchema.R @@ -0,0 +1,2 @@ +#' @param oracleTempSchema Should be used in Oracle to specify a schema where the user has write +#' privileges for storing temporary tables. diff --git a/man-roxygen/VocabularyDatabaseSchema.R b/man-roxygen/VocabularyDatabaseSchema.R new file mode 100644 index 00000000..6ea56b96 --- /dev/null +++ b/man-roxygen/VocabularyDatabaseSchema.R @@ -0,0 +1,3 @@ +#' @param vocabularyDatabaseSchema Schema name where your OMOP vocabulary format resides. +#' Note that for SQL Server, this should include both the database and +#' schema name, for example 'vocabulary.dbo'.