|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +#' Example: Basic Usage |
| 3 | +#' |
| 4 | +#' Demonstrates basic OMOPHub client usage including: |
| 5 | +#' - Client initialization |
| 6 | +#' - Getting a single concept |
| 7 | +#' - Basic search |
| 8 | +#' - Listing vocabularies |
| 9 | +#' |
| 10 | +#' Run with: Rscript inst/examples/basic_usage.R |
| 11 | + |
| 12 | +library(omophub) |
| 13 | + |
| 14 | +# ============================================================================ |
| 15 | +# Setup |
| 16 | +# ============================================================================ |
| 17 | + |
| 18 | +# Initialize client (uses OMOPHUB_API_KEY environment variable) |
| 19 | +# You can also pass the API key directly: |
| 20 | +# client <- omophub(api_key = "oh_your_api_key") |
| 21 | +client <- omophub() |
| 22 | + |
| 23 | +cat("OMOPHub R Client - Basic Usage Example\n") |
| 24 | +cat("======================================\n\n") |
| 25 | + |
| 26 | +# ============================================================================ |
| 27 | +# Get a Single Concept |
| 28 | +# ============================================================================ |
| 29 | + |
| 30 | +cat("1. Getting a single concept by ID\n") |
| 31 | +cat("---------------------------------\n") |
| 32 | + |
| 33 | +# Get Type 2 diabetes mellitus (SNOMED concept) |
| 34 | +concept <- client$concepts$get(201826) |
| 35 | + |
| 36 | +cat("Concept ID:", concept$concept_id, "\n") |
| 37 | +cat("Name:", concept$concept_name, "\n") |
| 38 | +cat("Vocabulary:", concept$vocabulary_id, "\n") |
| 39 | +cat("Domain:", concept$domain_id, "\n") |
| 40 | +cat("Concept Class:", concept$concept_class_id, "\n") |
| 41 | +cat("Concept Code:", concept$concept_code, "\n") |
| 42 | +cat("Standard Concept:", concept$standard_concept, "\n\n") |
| 43 | + |
| 44 | +# ============================================================================ |
| 45 | +# Basic Search |
| 46 | +# ============================================================================ |
| 47 | + |
| 48 | +cat("2. Searching for concepts\n") |
| 49 | +cat("-------------------------\n") |
| 50 | + |
| 51 | +# Search for diabetes-related concepts |
| 52 | +results <- client$search$basic("diabetes", page_size = 5) |
| 53 | + |
| 54 | +cat("Found concepts matching 'diabetes':\n") |
| 55 | +concepts <- results$data %||% results$concepts %||% results |
| 56 | +for (c in concepts) { |
| 57 | + cat(sprintf(" [%s] %s (%s)\n", |
| 58 | + c$concept_id, |
| 59 | + c$concept_name, |
| 60 | + c$vocabulary_id)) |
| 61 | +} |
| 62 | +cat("\n") |
| 63 | + |
| 64 | +# ============================================================================ |
| 65 | +# List Vocabularies |
| 66 | +# ============================================================================ |
| 67 | + |
| 68 | +cat("3. Listing available vocabularies\n") |
| 69 | +cat("---------------------------------\n") |
| 70 | + |
| 71 | +# Get first 10 vocabularies |
| 72 | +vocabs <- client$vocabularies$list(page_size = 10) |
| 73 | + |
| 74 | +cat("Available vocabularies:\n") |
| 75 | +vocab_list <- vocabs$data %||% vocabs$vocabularies %||% vocabs |
| 76 | +for (v in vocab_list) { |
| 77 | + cat(sprintf(" %s - %s\n", |
| 78 | + v$vocabulary_id, |
| 79 | + v$vocabulary_name)) |
| 80 | +} |
| 81 | +cat("\n") |
| 82 | + |
| 83 | +# ============================================================================ |
| 84 | +# Get Concept with Additional Information |
| 85 | +# ============================================================================ |
| 86 | + |
| 87 | +cat("4. Getting a concept with relationships\n") |
| 88 | +cat("---------------------------------------\n") |
| 89 | + |
| 90 | +# Get concept with relationships included |
| 91 | +concept_full <- client$concepts$get(201826, include_relationships = TRUE) |
| 92 | + |
| 93 | +cat("Concept:", concept_full$concept_name, "\n") |
| 94 | +if (!is.null(concept_full$relationships)) { |
| 95 | + cat("Number of relationships:", length(concept_full$relationships), "\n") |
| 96 | +} |
| 97 | +cat("\n") |
| 98 | + |
| 99 | +# ============================================================================ |
| 100 | +# Done |
| 101 | +# ============================================================================ |
| 102 | + |
| 103 | +cat("Done!\n") |
0 commit comments