Capr icon indicating copy to clipboard operation
Capr copied to clipboard

as.json no longer functioning on Cohort object in version 2.0.8

Open guuswilmink opened this issue 1 year ago • 3 comments

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.

guuswilmink avatar Jun 26 '24 14:06 guuswilmink

This error occurred due to using a named vector as an input rather than an unnamed vector of concept IDs. Closing issue.

guuswilmink avatar Jun 27 '24 10:06 guuswilmink

glad you sorted it out, thanks for posting an issue

mdlavallee92 avatar Jun 27 '24 11:06 mdlavallee92

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.

guuswilmink avatar Jun 27 '24 11:06 guuswilmink

may I ask what is name vector of concept IDs in this case?

JetKinTanAccelteam avatar Oct 30 '24 08:10 JetKinTanAccelteam

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

guuswilmink avatar Nov 04 '24 10:11 guuswilmink

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"’

erikwestlund avatar Feb 14 '25 01:02 erikwestlund

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

katy-sadowski avatar Feb 15 '25 01:02 katy-sadowski

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))

xj2193 avatar Feb 18 '25 03:02 xj2193

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, ...))
})

xj2193 avatar Feb 18 '25 03:02 xj2193

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 avatar Feb 18 '25 14:02 mdlavallee92

@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?

katy-sadowski avatar Feb 18 '25 15:02 katy-sadowski

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 avatar Feb 18 '25 15:02 mdlavallee92

@mdlavallee92 happy to support. I am still getting familiar with Capr but happy to learn/contribute more.

xj2193 avatar Feb 18 '25 15:02 xj2193

@xj2193 find me on OHDSI teams or email me so we can talk more

mdlavallee92 avatar Feb 18 '25 15:02 mdlavallee92