extras/example_createCohortSpecificationDocumentInWord.R

# install.packages('devtools')
# library(devtools)
# install.packages('here')
# library(here)
# devtools::install_github("gowthamrao/StudyManagement")
# devtools::install_github("davidgohel/officer")
# install.packages("snakecase")
require(officer)
require(StudyManagement)
require(snakecase)

a <- ("./template/MicrosoftOffice/word/style APA.docx")

# Officer style template - source from here
## https://github.com/gowthamrao/StudyManagement/raw/master/template/MicrosoftOffice/word/style%20APA.docx
styleAPA <- officer::read_docx(a)
# View(officer::styles_info(styleAPA))

# read data into R
baseUrl <- "http://api.ohdsi.org:80/WebAPI"
cohortId <- "1772958"
# the source is from http://atlas-demo.ohdsi.org/#/cohortdefinition/1772958
## see tab 'definition'.
## Also see tab 'Export' --> tab 'Text view'
## 'Text view' is the human readable version of the definition tab. If changes are made to the definition
## tab, the changes are propagated into the Text view tab.

# Project
## requirements - replicate textview in Word using officer package and native word format.
## all the data needed to create report will be retrieved into R using StudyManagement package.
## output reio
## it should be able to support Cohort Entry events, Inclusion criteria, cohort exit, censoring criteria.
## it should also render the concept-sets
## java script https://github.com/OHDSI/Atlas/tree/master/js/components/cohortdefinitionviewer



cohortSpecifications <- StudyManagement::getCohortSpecification(baseUrl = baseUrl, cohortId = cohortId)$parsed

# Example:
## Subsection heading
cohortDescription <- styleAPA %>%
                    officer::body_add_par(cohortSpecifications$name, style = 'Section Title') %>%
                    officer::body_add_par(paste('Cohort id',cohortSpecifications$id,
                                                 'was created on ', cohortSpecifications$createdDate,
                                                 'and last modified on', cohortSpecifications$modifiedDate,
                                                 '.'
                                                 , sep = " "
                                                 )
                                          , style = "Normal"
                    )

## Cohort entry events
primarySpecifications <- cohortSpecifications$expression$PrimaryCriteria
cohortDescription <- cohortDescription %>%
                    officer::body_add_par("People having any of the following:")

criteriaList <- list()
for (i in (1:length(primarySpecifications$CriteriaList))) {
  cohortDescription <- cohortDescription %>%
                        officer::body_add_par(paste("\t ",
                                                 "a",
                                                 snakecase::to_sentence_case(names(primarySpecifications$CriteriaList[[i]])),
                                                 "of:"
                                                 , sep = " "
                                                 )
                                           , style = "Normal"
                                          )

}

View(cohortSpecifications$expression$PrimaryCriteria$CriteriaList)

print(cohortDescription, "D:/output/cohort.docx")
gowthamrao/StudyManagement documentation built on March 9, 2020, 10:48 p.m.