OHDSI / CirceR

R package wrapper for CIRCE
https://ohdsi.github.io/CirceR/
6 stars 2 forks source link

HTML and PDF friendly rendering #8

Open msuchard opened 3 years ago

msuchard commented 3 years ago

I have been working on some wrapper functions that render cohorts and, importantly, their concept set tables nicely in both HTML and PDF (via knitr). These types of helper functions (made sufficiently generic) should someday find themselves into CirceR

printCohortDefinitionFromNameAndJson <- function(name, json) {

  obj <- CirceR::cohortExpressionFromJson(json)

  writeLines(paste("##", name, "\n"))

  # Print main definition
  markdown <- CirceR::cohortPrintFriendly(obj)
  writeLines(markdown)

  # Print concept sets
  if (knitr::is_latex_output()) {

    lapply(obj$conceptSets, function(conceptSet) {
      markdown <- CirceR::conceptSetPrintFriendly(conceptSet)
      rows <- unlist(strsplit(markdown, "\\r\\n"))
      rows <- gsub("^\\|", "", rows)
      header <- rows[1]
      data <- readr::read_delim(paste(rows[c(2,4:(length(rows)-2))], 
                                      collapse = '\n'), delim = '|',)

      header <- gsub("###", "### Concept:", header)
      writeLines(header)

      tab <- kable(data, linesep = "", booktabs = TRUE) %>%
        kable_styling(bootstrap_options = "striped", latex_options = "striped",
                      font_size = latex_table_font_size) %>%
        column_spec(1, width = "5em") %>%
        column_spec(2, width = "20em")

      writeLines(tab) 
    })

  } else {
    markdown <- CirceR::conceptSetListPrintFriendly(obj$conceptSets)
    markdown <- gsub("###", "### Concept:", markdown) 
    writeLines(markdown)
  }
}

What are your thoughts @chrisknoll ?