{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "R_get_started.ipynb", "provenance": [], "authorship_tag": "ABX9TyPkRcZlL+l67RshCOJXMEYs", "include_colab_link": true }, "kernelspec": { "name": "ir", "display_name": "R" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "MJCtIAZLTnfA", "colab_type": "text" }, "source": [ "## A) Mount to google drive## " ] }, { "cell_type": "code", "metadata": { "id": "rCEtur7fajGn", "colab_type": "code", "outputId": "f5f34c55-d21c-4d5e-857f-1b3bcfbfa15a", "colab": { "base_uri": "https://localhost:8080/", "height": 109 } }, "source": [ "cat(system('python3 -c \"from google.colab import drive\\ndrive.mount()\"', intern=TRUE), sep='\\n', wait=TRUE)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Warning message in system(\"python3 -c \\\"from google.colab import drive\\ndrive.mount()\\\"\", :\n", "“running command 'python3 -c \"from google.colab import drive\n", "drive.mount()\"' had status 1”\n" ], "name": "stderr" }, { "output_type": "stream", "text": [ "\n", "TRUE\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "T6aH0TahbghZ", "colab_type": "code", "colab": {} }, "source": [ "?system" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "ezwFft-tSSUW", "colab_type": "text" }, "source": [ "## B) Try out comorbidity calculation ##\n", "\n", "- import devtools\n", "- downloand from github\n", "- import comorbidity library" ] }, { "cell_type": "code", "metadata": { "id": "Ipk2vXhuSL0Q", "colab_type": "code", "colab": {} }, "source": [ "install.packages(\"devtools\")\n", "require(devtools)\n", "\n", "install_github( 'ellessenne/comorbidity' )\n", "\n", "# load the comorbidity package\n", "library(comorbidity)\n", "# set a seed for reproducibility\n", "set.seed(1)\n", "\n", "# simulate 50 ICD-10 codes for 5 individuals\n", "x <- data.frame(\n", " id = sample(1:5, size = 50, replace = TRUE),\n", " code = sample_diag(n = 50),\n", " stringsAsFactors = FALSE\n", ")\n", "x <- x[order(x$id, x$code), ]\n", "print(head(x, n = 15), row.names = FALSE)\n" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "e2jONWnvS3Lq", "colab_type": "text" }, "source": [ "\n", "*** All of below is under construction!***\n", "\n", "## c) EHR data ##" ] }, { "cell_type": "code", "metadata": { "id": "bcsRVS2UF7Wx", "colab_type": "code", "outputId": "ed52e96b-5b5a-46a0-edbc-52161143dcb1", "colab": { "base_uri": "https://localhost:8080/", "height": 109 } }, "source": [ "url='https://github.com/rOpenHealth/rEHR/blob/master/data-raw/example_tables.R'\n", "\n", "install.packages('bit')" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Installing package into ‘/usr/local/lib/R/site-library’\n", "(as ‘lib’ is unspecified)\n", "\n", "Warning message in install.packages(\"bit\"):\n", "“installation of package ‘bit’ had non-zero exit status”\n" ], "name": "stderr" } ] }, { "cell_type": "code", "metadata": { "id": "Z0dvjeOS4GJN", "colab_type": "code", "outputId": "dfbcd32d-cb39-4d6b-9f97-d86c8883ab75", "colab": { "base_uri": "https://localhost:8080/", "height": 668 } }, "source": [ "remotes::install_github(\"rOpenHealth/rEHR\")\n", "require(rEHR)\n", "\n", "## Use the simulated ehr files supplied with the package to build our database\n", "ehr_path <- dirname(system.file(\"ehr_data\", \"ehr_Clinical.txt\", package = \"rEHR\"))\n", "## create a new database connection to a temporary file\n", "db <- database(tempfile(fileext = \".sqlite\"))\n", "## Import multiple data files into the database\n", "import_CPRD_data(db, data_dir = ehr_path,\n", "filetypes = c(\"Clinical\", \"Consultation\",\n", "\"Patient\", \"Practice\",\n", "\"Referral\"),\n", "dateformat = \"%Y-%m-%d\",\n", "yob_origin = 1800,\n", "regex = \"ehr\",\n", "recursive = TRUE)\n", "## Individual files can also be added:\n", "add_to_database(db, files = system.file(\"ehr_data\", \"ehr_Therapy.txt\", package = \"rEHR\"),\n", "table_name = \"Therapy\", dateformat = \"%Y-%m-%d\")\n", "## Use the overloaded `head` function to view a list of\n", "## tables or the head of individual tables:\n", "head(db)\n", "\n", "url='https://github.com/rOpenHealth/rEHR/blob/master/data-raw/example_tables.R'\n" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Downloading GitHub repo rOpenHealth/rEHR@master\n", "\n" ], "name": "stderr" }, { "output_type": "stream", "text": [ "sqldf (NA -> 0.4-11 ) [CRAN]\n", "RSQLite (NA -> 2.2.0 ) [CRAN]\n", "xlsx (NA -> 0.6.3 ) [CRAN]\n", "bit64 (NA -> 0.9-7 ) [CRAN]\n", "bit (NA -> 1.1-15.2) [CRAN]\n", "rJava (NA -> 0.9-12 ) [CRAN]\n", "gsubfn (NA -> 0.7 ) [CRAN]\n", "chron (NA -> 2.3-55 ) [CRAN]\n", "tibble (2.1.3 -> 3.0.0 ) [CRAN]\n", "xlsxjars (NA -> 0.6.1 ) [CRAN]\n" ], "name": "stdout" }, { "output_type": "stream", "text": [ "Installing 10 packages: sqldf, RSQLite, xlsx, bit64, bit, rJava, gsubfn, chron, tibble, xlsxjars\n", "\n", "Installing packages into ‘/usr/local/lib/R/site-library’\n", "(as ‘lib’ is unspecified)\n", "\n" ], "name": "stderr" }, { "output_type": "error", "ename": "ERROR", "evalue": "ignored", "traceback": [ "Error: Failed to install 'rEHR' from GitHub:\n (converted from warning) installation of package ‘bit’ had non-zero exit status\nTraceback:\n", "1. remotes::install_github(\"rOpenHealth/rEHR\")", "2. install_remotes(remotes, auth_token = auth_token, host = host, \n . dependencies = dependencies, upgrade = upgrade, force = force, \n . quiet = quiet, build = build, build_opts = build_opts, build_manual = build_manual, \n . build_vignettes = build_vignettes, repos = repos, type = type, \n . ...)", "3. tryCatch(res[[i]] <- install_remote(remotes[[i]], ...), error = function(e) {\n . stop(remote_install_error(remotes[[i]], e))\n . })", "4. tryCatchList(expr, classes, parentenv, handlers)", "5. tryCatchOne(expr, names, parentenv, handlers[[1L]])", "6. value[[3L]](cond)" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "YrDGP56mSx1v", "colab_type": "text" }, "source": [ "## D) Query EHR records ##" ] }, { "cell_type": "code", "metadata": { "id": "xt3AO5sEAlAl", "colab_type": "code", "colab": {} }, "source": [ "diabetes_codes <- clinical_codes[clinical_codes$list == \"Diabetes\",]\n", "\n", "select_events(db, tab = \"Clinical\", columns = c(\"patid\", \"eventdate\", \"medcode\"), where = \"medcode %in% .(diabetes_codes$medcode) & eventdate < '2006-01-01' & eventdate >= '2005-01-01'\")\n" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "lePkHRF0Sqr9", "colab_type": "text" }, "source": [ "## E) Eurostat ##" ] }, { "cell_type": "code", "metadata": { "id": "NQ8LYoyVq7dF", "colab_type": "code", "outputId": "d642339d-1a4f-4aed-c71c-412500d90171", "colab": { "base_uri": "https://localhost:8080/", "height": 503 } }, "source": [ "install.packages(\"eurostat\")\n", "require(eurostat)\n", "\n", "library(eurostat)\n", "\n", "toc <- eurostat::get_eurostat_toc()\n", "\n", "df <- fetch_healthdata(resource=\"hosp\", filter=list(addr_city=\"SAN FRANCISCO\"))\n", "\n" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Installing package into ‘/usr/local/lib/R/site-library’\n", "(as ‘lib’ is unspecified)\n", "\n", "also installing the dependencies ‘e1071’, ‘units’, ‘classInt’, ‘sf’\n", "\n", "\n", "Warning message in install.packages(\"eurostat\"):\n", "“installation of package ‘e1071’ had non-zero exit status”\n", "Warning message in install.packages(\"eurostat\"):\n", "“installation of package ‘units’ had non-zero exit status”\n", "Warning message in install.packages(\"eurostat\"):\n", "“installation of package ‘classInt’ had non-zero exit status”\n", "Warning message in install.packages(\"eurostat\"):\n", "“installation of package ‘sf’ had non-zero exit status”\n", "Warning message in install.packages(\"eurostat\"):\n", "“installation of package ‘eurostat’ had non-zero exit status”\n", "Loading required package: eurostat\n", "\n", "Warning message in library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :\n", "“there is no package called ‘eurostat’”\n" ], "name": "stderr" }, { "output_type": "error", "ename": "ERROR", "evalue": "ignored", "traceback": [ "Error in library(eurostat): there is no package called ‘eurostat’\nTraceback:\n", "1. library(eurostat)" ] } ] } ] }