### SQL: Populating CDM_SOURCE Table Example Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/sqlScripts.html This SQL script provides an example of how to populate the CDM_SOURCE table, which is a prerequisite for running the OHDSI Data Quality Dashboard. It includes essential metadata about the data source. ```sql -- Example ETL Script ---------------------- ### CDM_SOURCE Table The script below is an example for how to fill in the CDM_SOURCE table. This table is required for the [Data Quality Dashboard](https://github.com/OHDSI/DataQualityDashboard) package to run. ``` -------------------------------- ### Install CDM R Package and List Supported Dialects/Versions Source: https://github.com/ohdsi/commondatamodel/blob/main/rmd/cdmRPackage.rmd This snippet shows how to install the CommonDataModel R package from GitHub and then lists the supported SQL dialects and CDM versions available within the package. It requires the `devtools` package for installation. ```r ## First, install the package from GitHub install.packages("devtools") devtools::install_github("OHDSI/CommonDataModel") ## List the currently supported SQL dialects CommonDataModel::listSupportedDialects() ## List the currently supported CDM versions CommonDataModel::listSupportedVersions() ``` -------------------------------- ### Install and Use CDM R Package Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/cdmRPackage.html Installs the CDM R package from GitHub and demonstrates basic usage like listing supported dialects and versions. This is a prerequisite for generating DDLs or executing them directly. ```r install.packages("devtools") devtools::install_github("OHDSI/CommonDataModel") CommonDataModel::listSupportedDialects() CommonDataModel::listSupportedVersions() ``` -------------------------------- ### Instantiate CDM Tables Directly with R Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/cdmRPackage.html Installs the `DatabaseConnector` package and demonstrates how to connect to a SQL client and instantiate CDM tables directly using the `executeDdl` function. This requires an existing empty schema. ```r devtools::install_github("DatabaseConnector") cd <- DatabaseConnector::createConnectionDetails(dbms = "postgresql", server = "localhost/ohdsi", user = "postgres", password = "postgres", pathToDriver = "/pathToDriver" ) CommonDataModel::executeDdl(connectionDetails = cd, cdmVersion = "5.4", cdmDatabaseSchema = "ohdsi_demo") ``` -------------------------------- ### Instantiate CDM Tables Directly with executeDdl Source: https://github.com/ohdsi/commondatamodel/blob/main/rmd/cdmRPackage.rmd This function connects to a SQL client using `DatabaseConnector` and instantiates the CDM tables directly in the specified schema. It requires prior installation of the `DatabaseConnector` package and valid connection details. ```r ### 2a. To start, you need to download DatabaseConnector in order to connect to your database. devtools::install_github("DatabaseConnector") cd <- DatabaseConnector::createConnectionDetails(dbms = "postgresql", server = "localhost/ohdsi", user = "postgres", password = "postgres", pathToDriver = "/pathToDriver" ) CommonDataModel::executeDdl(connectionDetails = cd, cdmVersion = "5.4", cdmDatabaseSchema = "ohdsi_demo" ) ``` -------------------------------- ### Install DatabaseConnector Package from GitHub Source: https://github.com/ohdsi/commondatamodel/blob/main/README.md Installs the DatabaseConnector R package from GitHub. This package is required for establishing connections to various SQL databases. ```r devtools::install_github("ohdsi/DatabaseConnector") ``` -------------------------------- ### Install CommonDataModel R Package Source: https://context7.com/ohdsi/commondatamodel/llms.txt Installs the CommonDataModel R package from the OHDSI GitHub repository using the devtools package. Ensure devtools is installed first, then load the CommonDataModel package after successful installation. ```r # Install devtools if not already installed install.packages("devtools") # Install CommonDataModel package from GitHub devtools::install_github("OHDSI/CommonDataModel") # Load the package library(CommonDataModel) ``` -------------------------------- ### Configure and Initialize Highlight.js Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/useCaseY.html This JavaScript code configures and initializes Highlight.js, a syntax highlighting library. It customizes the available languages and triggers the highlighting process when the page is loaded or fully ready. This ensures code blocks are displayed with correct syntax formatting. ```javascript if (window.hljs) { hljs.configure({languages: []}); hljs.initHighlightingOnLoad(); if (document.readyState && document.readyState === "complete") { window.setTimeout(function() { hljs.initHighlighting(); }, 0); } } ``` -------------------------------- ### Set up jQuery UI Autocomplete Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/site_libs/jqueryui-1.13.2/index.html Sets up the autocomplete widget on an element with the ID 'autocomplete'. It uses a predefined array of tags as the data source for suggestions. ```javascript var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; $( "#autocomplete" ).autocomplete({ source: availableTags }); ``` -------------------------------- ### Install CommonDataModel Package from GitHub Source: https://github.com/ohdsi/commondatamodel/blob/main/README.md Installs the CommonDataModel R package from GitHub using the devtools package. This is a prerequisite for using the package's functions. ```r install.packages("devtools") devtools::install_github("OHDSI/CommonDataModel") ``` -------------------------------- ### Initialize jQuery UI Slider Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/site_libs/jqueryui-1.13.2/index.html Initializes the slider widget on an element with the ID 'slider'. It is configured as a range slider with predefined starting values. ```javascript $( "#slider" ).slider({ range: true, values: [ 17, 67 ] }); ``` -------------------------------- ### Initialize Google Analytics Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/cdm531.html This snippet initializes Google Analytics by pushing the 'js' command with the current date and then configuring the tracker with a specific tracking ID. ```javascript window.dataLayer = window.dataLayer || []; function gtag(){ dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'G-37JJNBGH0H'); ``` -------------------------------- ### Translate Raw DDL Strings with SqlRender Source: https://context7.com/ohdsi/commondatamodel/llms.txt Demonstrates how to use the SqlRender package to translate raw OHDSI-SQL DDL strings into a specific database dialect. This involves rendering the SQL with parameters and then translating it, allowing for programmatic generation of dialect-specific DDL files. ```r # Use SqlRender to translate to specific dialect library(SqlRender) ddlString <- CommonDataModel::createDdl(cdmVersion = "5.4") renderedSql <- SqlRender::render( sql = ddlString, cdmDatabaseSchema = "my_schema", targetDialect = "postgresql" ) translatedSql <- SqlRender::translate( sql = renderedSql, targetDialect = "postgresql" ) writeLines(translatedSql, "/tmp/custom_ddl.sql") ``` -------------------------------- ### List Supported OMOP CDM Versions Source: https://context7.com/ohdsi/commondatamodel/llms.txt Queries the installed CommonDataModel package to determine which OMOP CDM versions it supports. This is useful for conditional logic in scripts to ensure compatibility with specific CDM versions. ```r # List all supported CDM versions supportedVersions <- CommonDataModel::listSupportedVersions() print(supportedVersions) # Returns: c("5.3", "5.4") # Use in conditional logic if ("5.4" %in% CommonDataModel::listSupportedVersions()) { cdmVersion <- "5.4" } else { cdmVersion <- "5.3" } ``` -------------------------------- ### Initialize Tabsets (JavaScript) Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/cdm53.html This JavaScript code initializes tabset functionality for navigation elements, commonly used in documentation frameworks. It enables dropdown behavior for tabs, allowing users to switch between content panes. ```javascript $(document).ready(function () { window.buildTabsets("TOC"); }); $(document).ready(function () { $('.tabset-dropdown > .nav-tabs > li').click(function () { $(this).parent().toggleClass('nav-tabs-open'); }); }); ``` -------------------------------- ### SQL: Grouping Drug Exposures into Sub-Exposures Source: https://github.com/ohdsi/commondatamodel/blob/main/docs/sqlScripts.html This SQL code groups overlapping drug exposures for a given person and drug concept into consolidated sub-exposures. It calculates the start and end dates of these sub-exposures and the total count of exposures within each. This is a preliminary step before calculating drug eras. ```sql , cteSubExposures(row_number, person_id, drug_concept_id, drug_sub_exposure_start_date, drug_sub_exposure_end_date, drug_exposure_count) AS ( SELECT ROW_NUMBER() OVER (PARTITION BY person_id, drug_concept_id, drug_sub_exposure_end_date ORDER BY person_id) , person_id, drug_concept_id, MIN(drug_exposure_start_date) AS drug_sub_exposure_start_date, drug_sub_exposure_end_date, COUNT(*) AS drug_exposure_count FROM cteDrugExposureEnds GROUP BY person_id, drug_concept_id, drug_sub_exposure_end_date --ORDER BY person_id, drug_concept_id ) ```