Capr
Capr copied to clipboard
as.json no longer functioning on Cohort object in version 2.0.8
Whilst following the user guide, I came across the following error:
giBleedCohortJson <- as.json(giBleedCohort) Error: unable to find an inherited method for function ‘as.json’ for signature ‘x = "Cohort"’.
Downgrading to version 2.0.7 shows the problem only arises in version 2.0.8, as version 2.0.7 runs as.json as intended.
EDIT: version 2.0.7 actually seems to present a similar issue. The problem seems to start from version 2.0.6 onward.
This error occurred due to using a named vector as an input rather than an unnamed vector of concept IDs. Closing issue.
glad you sorted it out, thanks for posting an issue
Apologies for the confusion. The as.json function does appear broken after all, producing the same error provided previously (regardless of using a named or unnamed vector). Downgrading to 2.0.5 alleviates the problem for both named and unnamed vectors (though named vectors produce a faulty json down the line).
If necessary, I can provide the exact code I'm using, though I am able to reproduce the error by following the user guide to the letter.
may I ask what is name vector of concept IDs in this case?
Hi @JetKinTanAccelteam, I originally thought the issue was caused by using a vector with named values and mistakenly thought it was resolved by using a vector with unnamed values. The same issue unfortunately persisted. The error should be reproduced when following these steps: https://ohdsi.github.io/Capr/articles/Using-Capr.html
I run into the same issue. R 4.3.3. MacOS. Capr 2.0.8.
> GIBleed <- cs(descendants(192671), name = "GIbleed")
>
> GIBleed
── <Capr Concept Set> GIbleed ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# A tibble: 1 × 9
conceptId conceptCode conceptName domainId vocabularyId standardConcept includeDescendants isExcluded includeMapped
<int> <chr> <chr> <chr> <chr> <chr> <lgl> <lgl> <lgl>
1 192671 "" "" "" "" "" TRUE FALSE FALSE
>
> giBleedCohort <- cohort(
+ entry = entry(
+ conditionOccurrence(GIBleed),
+ observationWindow = continuousObservation(0L, 0L),
+ primaryCriteriaLimit = "First"
+ ),
+ exit = exit(
+ endStrategy = observationExit()
+ )
+ )
>
> as.json(giBleedCohort)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘as.json’ for signature ‘"Cohort"’
It seems the as.json method was removed from Cohort in this PR (maybe by mistake?): https://github.com/OHDSI/Capr/pull/78/commits/6fc04f37b3fa1cbf142f87fedeb5f540e774537f#diff-0741d989e42ec28fcfd7340d5cc4a49fcf13dbd88c8efcb49d22935e30bbadbfL388-L392
I also encountered the same error while running the Using-Capr.Rmd file.
> giBleedCohortJson <- as.json(giBleedCohort)
Error: unable to find an inherited method for function ‘as.json’ for signature ‘x = "Cohort"’
It also affects the function:
> assertCohortCompiles(ch)
Error: unable to find an inherited method for function ‘as.json’ for signature ‘x = "Cohort"’
However this one works:
diclofenac <- getConceptSetDetails(diclofenac, con, vocabularyDatabaseSchema = "main")
cat(as.json(diclofenac))
Adding this to cohort.R file under Coerce section fixed the problem:
#' 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"))
#' @rdname as.json
#' @aliases as.json,ConceptSet-method
setMethod("as.json", "ConceptSet", function(x, pretty = TRUE, ...){
items <- list(items = lapply(x@Expression, as.list))
as.character(jsonlite::toJSON(x = items, pretty = pretty, auto_unbox = TRUE, ...))
})
#' @rdname as.json
#' @aliases as.json,Cohort-method
setMethod("as.json", "Cohort", function(x, pretty = TRUE, ...) {
# Use the existing toCirce function to get the proper structure
circe_data <- toCirce(x)
# Convert to JSON using jsonlite and ensure it's a character string
as.character(jsonlite::toJSON(x = circe_data, pretty = pretty, auto_unbox = TRUE, ...))
})
Thanks for pointing this out! I think the plan was to switch this to the compile generic function but some old code made its way into the vignette. @katy-sadowski I will accept your PR for release so that it works again and add in your comment @xj2193 (that was the old export of the method).
@mdlavallee92 i didn't open a PR for this (yet), wanted to check first if the intention was to remove this and replace with something else. but i can add this to my open PR with the concept set change to include this?
I added it to your branch for now. Actively looking for a maintainer to clean this all up in the future (in the docs and exported functions). @xj2193 @erikwestlund any interest on volunteering to support Capr?
@mdlavallee92 happy to support. I am still getting familiar with Capr but happy to learn/contribute more.
@xj2193 find me on OHDSI teams or email me so we can talk more