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

Get R check passing #9

Merged
merged 5 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/R_CMD_check_Hades.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
use-public-rspm: true

- uses: r-lib/actions/setup-tinytex@v1

Expand Down
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: Capr
Title: Cohort Definition Application Programming
Version: 2.0.1
Version: 2.0.2
Authors@R: c(
person("Martin", "Lavallee", , "mdlavallee92@gmail.com", role = c("aut", "cre")),
person("Martin", "Lavallee", , "adam.black@odysseusinc.com", role = c("aut", "cre")),
person("Adam", "Black", , "black@ohdsi.org", role = "aut")
)
Description: Provides a programming language for defining OHDSI cohort definitions in R to use in studies for Observational
Expand All @@ -11,9 +11,9 @@ Description: Provides a programming language for defining OHDSI cohort definitio
'Capr' functions can be used to create, save, and load component parts to a cohort definition allowing
R programmers to easily reuse cohort logic. 'Capr' provides tools to create a large number of OHDSI cohorts
programmatically while also helping bridge the gap between human readable descriptions of clinical phenotypes
and their computational implmentation.
and their computational implementation.
License: Apache License (>= 2)
URL: https://ohdsi.github.io/Capr, https://github.com/OHDSI/Capr
URL: https://ohdsi.github.io/Capr, https://github.com/OHDSI/Capr/
BugReports: https://github.com/OHDSI/Capr/issues
Encoding: UTF-8
RoxygenNote: 7.2.3
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
S3method(compile,Cohort)
export("%>%")
export(age)
export(as.json)
export(atLeast)
export(atMost)
export(attrition)
export(bt)
export(censoringEvents)
export(cohort)
export(compile.Cohort)
export(compile)
export(condition)
export(conditionEra)
export(continuousObservation)
Expand Down Expand Up @@ -68,6 +67,7 @@ export(withAtMost)
export(writeCohort)
export(writeConceptSet)
exportMethods("==")
exportMethods(compile)
import(dplyr)
importFrom(generics,compile)
importFrom(magrittr,"%>%")
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Capr 2.0.2
==========
- fix tests for CRAN

Capr 2.0.1
==========
- additions and corrections to vignettes and documentation
Expand Down
1 change: 1 addition & 0 deletions R/attributes-op.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ setMethod("nbt", "Date", function(x, y) {
#' Function to create age attribute
#' @param op an opAttribute object that is either numeric or integer that defines the logical
#' operation used to determine eligible patient age
#' @return An age attribute that can be used in a cohort definition
#' @export
age <- function(op) {

Expand Down
111 changes: 30 additions & 81 deletions R/cohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ setClass("CohortExit",
)

## CohortEra----

setClass("CohortEra",
slots = c(
eraDays = "integer",
Expand Down Expand Up @@ -115,6 +114,7 @@ entry <- function(...,
#'
#' @param ... Capr groups
#' @param expressionLimit how to limit initial events per person either First, All, or Last
#' @return A cohort attrition object that can be used in a cohort definition
#' @export
attrition <- function(..., expressionLimit = c("First", "All", "Last")) {

Expand All @@ -126,11 +126,10 @@ attrition <- function(..., expressionLimit = c("First", "All", "Last")) {

}



#' Function that creates a cohort exit object
#' @param endStrategy the endStrategy object to specify for the exit
#' @param censor the censoring criteria to specify for the exit
#' @return A cohort exit object that can be used in a cohort definition
#' @export
exit <- function(endStrategy, censor = NULL){
if (is.null(censor)) {
Expand Down Expand Up @@ -318,50 +317,19 @@ toCirce <- function(cd) {
return(cdCirce)
}

#' Compile a Capr object to json
#'
#' @param object A Capr object such as a cohort, list of cohorts, or concept set.
#' @param ... Arguments passed on to jsonlite::toJSON.
#' e.g. `pretty = TRUE` for nicely formatted json.
#'
#' @return The json representation of the Capr object
#' @export
setGeneric("compile", function(object, ...) { standardGeneric("compile") })

# cs1 <- cs(descendants(exclude(436665),440383,442306,4175329))
# cs2 <- cs(descendants(exclude(436665),440383,4175329))


# #' @export
# setMethod("as.list", "Cohort", function (x, ...) {
#
# # entry events
# entryConceptSets <- purrr::map(x@entry@entryEvents, collectConceptSets)
#
# # re-number concept sets
# allConceptSets <- entryConceptSets # need to add criteria concept sets
#
# r <- dedupConceptSets(allConceptSets)
# lookup <- r$lookup
# uniqueConceptSets <- purrr::map(r$uniqueConceptSets, function(x) {
# x <- as.list(x)
# x$id <- unname(lookup[x$id])
# x
# })
#
# cohortList <- list(
# ConceptSets = uniqueConceptSets,
# PrimaryCriteria = list(CriteriaList = purrr::map(x@entry@entryEvents, ~lst(!!.@domain := list(CodesetId = .@conceptSet@id))),
# ObservationWindow = list(priorDays = x@entry@observationWindow@priorDays, postDays = x@entry@observationWindow@postDays),
# PrimaryCriteriaLimit = list(Type = x@entry@primaryCriteriaLimit)
# ),
# QualifiedLimit = list(Type = x@entry@qualifiedLimit),
# ExpressionLimit = list(Type = x@attrition@expressionLimit),
# InclusionRules = x@attrition@rules, # TODO use map(rules, as.list)
# CensoringCriteria = x@exit@censor,
# CollapseSettings = list(collapseType = "ERA", EraPad = x@era@eraDays),
# CensorWindow = list() # TODO implement censor window
# )
#
# cohortList$PrimaryCriteria$CriteriaList <- purrr::map(cohortList$PrimaryCriteria$CriteriaList,
# function(criteria) {
# criteria[[1]]$CodesetId <- unname(lookup[criteria[[1]]$CodesetId])
# criteria
# })
# cohortList
# })

compile.Cohort <- function(object, ...) {
as.character(jsonlite::toJSON(toCirce(object), auto_unbox = TRUE, ...))
}

#' Compile a Capr cohort to json
#'
Expand All @@ -370,26 +338,32 @@ toCirce <- function(cd) {
#' e.g. `pretty = TRUE` for nicely formatted json.
#'
#' @return The json representation of Capr cohorts
#' @export
#' @importFrom generics compile
#' @exportS3Method compile Cohort
#' @export
#' @rdname compile-methods
#' @examples
#' \dontrun{
#' ch <- cohort(condition(cs(1,2)))
#' compile(ch)
#' }
compile.Cohort <- function(object, ...) {
as.character(jsonlite::toJSON(toCirce(object), auto_unbox = TRUE, ...))
}

setMethod("compile", "Cohort", compile.Cohort)

compile.ConceptSet <- function(object, ...) {
x <- list(items = lapply(object@Expression, as.list))
as.character(jsonlite::toJSON(x, auto_unbox = TRUE, ...))
}

#' @rdname as.json
#' @aliases as.json,Cohort-method
setMethod("as.json", "Cohort", function(x, pretty = TRUE, ...) {
as.character(jsonlite::toJSON(toCirce(x), auto_unbox = TRUE, pretty = pretty, ...))
})
#' Compile a Capr Concept Set to json
#'
#' @export
#' @rdname compile-methods
#' @param object A Capr Concept Set created with `cs`
#' @param ... Arguments passed on to jsonlite::toJSON.
#' e.g. `pretty = TRUE` for nicely formatted json.
#'
#' @return The json representation of Capr cohorts
setMethod("compile", "ConceptSet", compile.ConceptSet)

setMethod("show", "Cohort", function(object) {
# TODO make this pretty on the console
Expand All @@ -400,9 +374,7 @@ setMethod("show", "Cohort", function(object) {
#'
#' @param x A Capr cohort
#' @param path The name of the file to create
#'
#' @export
#'
#' @examples
#' \dontrun{
#' cs1 <- cs(descendants(exclude(436665),440383,442306,4175329))
Expand All @@ -415,9 +387,6 @@ writeCohort <- function(x, path) {
checkmate::assertClass(x, "Cohort")
checkmate::assertCharacter(path, len = 1, min.chars = 1, pattern = "\\.json$")

# ParallelLogger::logInfo(
# "Cohort written to", path
# )
toCirce(x) %>%
jsonlite::write_json(
path = path,
Expand All @@ -440,25 +409,5 @@ generateCaprTemplate <- function(file, .capr) {

#generate cohort from template
.capr(conceptSet)

}

# writeCohort <- function(x, path, ...) {
# checkmate::assertClass(x, "Cohort")
# checkmate::assertCharacter(path, len = 1, min.chars = 1, pattern = "\\.json$")
# # check that concept set details are filled in
# check <- unlist(x$ConceptSets, recursive = TRUE)
# if (any(check[grepl( "CONCEPT_NAME|STANDARD_CONCEPT", names(check))] == "")) {
# rlang::abort("Concept set details are missing. Fill in concept set details using `getConceptSetDetails()`")
# }
# jsonlite::write_json(x = as.list(x), path = path, auto_unbox = TRUE, pretty = TRUE, ...)
# }









21 changes: 3 additions & 18 deletions R/conceptSet.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,6 @@ setMethod("as.list", "ConceptSet", function(x){
'expression' = list('items' = lapply(x@Expression, as.list)))
})

#' Coerce Capr object to json
#' @param x the capr object
#' @param pretty a toggle to make the json look nice, part of jsonlite
#' @param ... additional arguments passes to jsonlite::toJSON
#' @export
#' @docType methods
setGeneric("as.json", function(x, pretty = TRUE, ...) standardGeneric("as.json"))

setMethod("as.json", "ConceptSet", function(x, pretty = TRUE, ...){
items <- list(items = lapply(x@Expression, as.list))
jsonlite::toJSON(x = items, pretty = pretty, auto_unbox = TRUE, ...)
})

#' Save a concept set as a json file
#'
#' The resulting concept Set JSON file can be imported into Atlas.
Expand Down Expand Up @@ -501,11 +488,9 @@ readConceptSet <- function(path, name, id = NULL) {
conceptCode = df[["concept_code"]] %||% df[["concept code"]] %||% "" %>% as.character(),
domainId = df[["domain_id"]] %||% df[["domain"]] %||% "" %>% as.character(),
vocabularyId = df[["vocabulary_id"]] %||% df[["vocabulary"]] %||% "" %>% as.character(),
conceptClassId = df[["concept_class_id"]] %||% "" %>% as.character()
) %>%
dplyr::mutate(
dplyr::across(.data$conceptName:.data$conceptClassId, ~tidyr::replace_na(.x, "")) #convert na to ""
)
conceptClassId = df[["concept_class_id"]] %||% "" %>% as.character()) %>%
dplyr::mutate_if(is.character, ~tidyr::replace_na(.x, ""))

conceptList <- purrr::pmap(conceptDf, newConcept)
}

Expand Down
3 changes: 3 additions & 0 deletions R/criteria.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ exactly <- function(x,
}

#' Function to enumerate an minimal count of occurrences
#'
#' @param x the integer counting the number of occurrences
#' @param query a query object that provides context to the clinical event of interest
#' @param aperture an eventAperture object that shows the temporal span where the event is to be observed
#' relative to the index event
#' @return A criteria that can be used in a cohort definition
#' @export
atLeast <- function(x,
query,
Expand All @@ -126,6 +128,7 @@ atLeast <- function(x,
#' @param query a query object that provides context to the clinical event of interest
#' @param aperture an eventAperture object that shows the temporal span where the event is to be observed
#' relative to the index event
#' @param A criteria that can be used in a cohort definition
#' @export
atMost <- function(x,
query,
Expand Down
2 changes: 1 addition & 1 deletion R/query.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#' An S4 class for a Circe Query
#'
#' A query is a medical concept that can be extracted from a database through a 'where' clause in a SQL statement.
#' A query is a medical concept that can be extracted from a database through a 'where' clause in a 'SQL' statement.
#' This includes concepts.
#'
#' @slot domain The domain to search (e.g. "Condition", "Drug", "Measurement", etc)
Expand Down
14 changes: 7 additions & 7 deletions docs/404.html

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

Loading