You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/read-cases.Rmd
+44-39Lines changed: 44 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,8 @@ editor_options:
9
9
:::::::::::::::::::::::::::::::::::::: questions
10
10
11
11
- Where do you usually store your outbreak data?
12
-
-How many different data formats can you use for analysis?
13
-
- Can you import data from databases and health APIs?
12
+
-What data formats do you use for analysis?
13
+
- Can you import data from databases and Health Information Systems (HIS) through their APIs?
14
14
::::::::::::::::::::::::::::::::::::::::::::::::
15
15
16
16
::::::::::::::::::::::::::::::::::::: objectives
@@ -30,9 +30,9 @@ This episode requires you to be familiar with:
30
30
31
31
## Introduction
32
32
33
-
The initial step in outbreak analysis typically involves importing the target dataset into the `R` environment from either a local source (like a file on your computer) or external source (like a database). Outbreak data can be stored in diverse formats, relational database management systems (RDBMS), or health information systems (HIS), such as [REDCap](https://www.project-redcap.org/) and [DHIS2](https://dhis2.org/), which provide application program interfaces (APIs) to the database systems so verified users can easily add and access data entries. The latter option is particularly well-suited for collecting and storing large-scal institutional health data. This episode will elucidate the process of reading cases from these sources.
33
+
The initial step in outbreak analysis typically involves importing the target dataset into the `R` environment from either a local source (like a file on your computer) or external source (like a database). Outbreak data can be stored in diverse formats, relational database management systems (RDBMS), or health information systems (HIS), such as [REDCap](https://www.project-redcap.org/) and [DHIS2](https://dhis2.org/), which provide application program interfaces (APIs) to the system's database so verified users can easily add and fetch data entries. The latter option is particularly well-suited for collecting and storing large-scale institutional health data. This episode will elucidate the process of reading cases from these sources.
34
34
35
-
Let's start by loading the package `{rio}` to read data and the package `{here}` to easily find a file path within your RStudio project. We'll use the pipe `%>%` to easily connect some of their functions, including functions from the data formatting package `{dplyr}`. We'll therefore call the tidyverse package, which includes both the pipe and `{dplyr}`:
35
+
Let's start by loading the `{rio}`package to read data and the `{here}`package to easily find a file path within your RStudio project. We'll use the pipe `%>%`operator from the `{magrittr}` package to easily connect some of their functions, including functions from the data formatting package `{dplyr}`. We'll therefore call the `{tidyverse}` package, which includes both `{magrittr}`and `{dplyr}`.
36
36
37
37
```{r,eval=TRUE,message=FALSE,warning=FALSE}
38
38
# Load packages
@@ -43,13 +43,17 @@ library(here) # for easy file referencing
43
43
44
44
::::::::::::::::::: checklist
45
45
46
-
### The double-colon
46
+
### The double-colon (`::`) operator
47
47
48
-
The double-colon `::` in R lets you call a specific function from a package without loading the entire package into the current environment.
48
+
The `::` in R lets you access functions or objects from a specific package without attaching the entire package to the search path. It offers several important
49
+
advantages including the followings:
49
50
50
-
For example, `dplyr::filter(data, condition)` uses `filter()` from the `{dplyr}` package, without having to use `library(dplyr)` at the start of a script.
51
+
* Telling explicitly which package a function comes from, reducing ambiguity and potential conflicts when several packages have functions with the same name.
52
+
* Allowing to call a function from a package without loading the whole package
53
+
with library().
51
54
52
-
This help us remember package functions and avoid namespace conflicts (i.e. when two different packages include functions with the same name, so R does not know which to use).
55
+
For example, the command `dplyr::filter(data, condition)` means we are calling
56
+
the `filter()` function from the `{dplyr}` package.
53
57
54
58
:::::::::::::::::::
55
59
@@ -66,15 +70,15 @@ This help us remember package functions and avoid namespace conflicts (i.e. when
66
70
67
71
## Reading from files
68
72
69
-
Several packages are available for importing outbreak data stored in individual files into `R`. These include [rio](https://gesistsa.github.io/rio/), [readr](https://readr.tidyverse.org/) from the `tidyverse`, [io](https://bitbucket.org/djhshih/io/src/master/), [ImportExport](https://cran.r-project.org/web/packages/ImportExport/index.html), and [data.table](https://rdatatable.gitlab.io/data.table/). Together, these packages offer methods to read single or multiple files in a wide range of formats.
73
+
Several packages are available for importing outbreak data stored in individual files into `R`. These include [{rio}](https://gesistsa.github.io/rio/), [{readr}](https://readr.tidyverse.org/) from the `{tidyverse}`, [{io}](https://bitbucket.org/djhshih/io/src/master/), [{ImportExport}](https://cran.r-project.org/web/packages/ImportExport/index.html), [{data.table}](https://rdatatable.gitlab.io/data.table/), and similar functions from the {base} R package. Together, these packages offer methods to read single or multiple files in a wide range of formats.
70
74
71
-
The below example shows how to import a `csv` file into `R` environment using `{rio}` package. We use the `{here}` package to tell R to look for the file in the `data/` folder of your project, and `as_tibble()` to convert into a tidier format for subsequent analysis in R.
75
+
The below example shows how to import a `csv` file into `R` environment using `{rio}` package. We use the `{here}` package to tell R to look for the file in the `data/` folder of your project, and `as_tibble()` to convert it into a tidier format for subsequent analysis in R.
72
76
73
77
```{r,eval=FALSE,echo=TRUE}
74
78
# read data
75
-
# e.g., the path to our file is data/raw-data/ebola_cases_2.csv then:
79
+
# e.g., if the path to our file is "data/raw-data/ebola_cases_2.csv" then:
Similarly, you can import files of other formats such as `tsv`, `xlsx`, ... etc.
101
+
Similarly, you can import files of other formats such as `tsv`, `xlsx`, ... etc
102
+
using the same function.
99
103
100
104
:::::::::::::::::::: checklist
101
105
102
106
### Why should we use the {here} package?
103
107
104
-
The `{here}` package is designed to simplify file referencing in R projects by providing a reliable way to construct file paths relative to the project root. The main reason to use it is **Cross-Environment Compatibility**.
108
+
The `{here}` package is designed to simplify file referencing in R projects by providing a reliable way to construct file paths relative to the project root. The main reason to use it is to ensure **Cross-Environment Compatibility**.
105
109
106
110
It works across different operating systems (Windows, Mac, Linux) without needing to adjust file paths.
107
111
108
112
- On Windows, paths are written using backslashes ( `\` ) as the separator between folder names: `"data\raw-data\file.csv"`
109
-
- On Unix based operating system such as macOS or Linux the forward slash ( `/` ) is used as the path separator: `"data/raw-data/file.csv"`
113
+
- On Unix based operating system such as macOS or Linux the forward slash ( `/` ) is used as separator between folder names: `"data/raw-data/file.csv"`
110
114
111
115
The `{here}` package is ideal for adding one more layer of reproducibility to your work. If you are interested in reproducibility, we invite you to [read this tutorial to increase the openess, sustainability, and reproducibility of your epidemic analysis with R](https://epiverse-trace.github.io/research-compendium/)
112
116
@@ -122,14 +126,14 @@ Can you read data from a compressed file in `R`? Download this [zip file](https:
122
126
::::::::::::::::: hint
123
127
124
128
You can check the [full list of supported file formats](https://gesistsa.github.io/rio/#supported-file-formats)
125
-
in the `{rio}` package on the package website. To expand {rio} to the full range of support for import and export formats run:
129
+
from the `{rio}` package website. To expand {rio} to its current unsupported file formats, run:
The [DBI](https://dbi.r-dbi.org/) package serves as a versatile interface for interacting with database management systems (DBMS) across different back-ends or servers. It offers a uniform method for accessing and retrieving data from various database systems.
154
+
The [{DBI}](https://dbi.r-dbi.org/) package serves as a versatile interface for interacting with relational database management systems (DBMS) across different back-ends or servers. It offers a uniform method for accessing and retrieving data from various database systems.
151
155
152
156
::::::::::::: discussion
153
157
154
-
### When to read directly from a database?
158
+
### Advantages of reading data directly from a database?
155
159
156
-
We can use database interface packages to optimize memory usage. If we process the database with "queries" (e.g., select, filter, summarise) before extraction, we can reduce the memory load in our RStudio session. Conversely, conducting all data manipulation outside the database management system by loading the full dataset into R can use up much more computer memory (i.e. RAM) than is feasible on a local machine, which can lead RStudio to slow down or even freeze.
160
+
We can use database interface packages to optimize the amount of memory used during our R session. If we query the database with data filtration requests (e.g., select, filter, summarise) before extraction, we can reduce the memory load in our RStudio session. Conversely, conducting all data manipulation outside the database management system by loading the full dataset into R can use up much more computer memory (i.e. RAM) than is feasible on a local machine, which can lead RStudio to slow down or even freeze.
157
161
158
-
External relational database management systems (RDBMS) also have the advantage that multiple users can access, store and analyse parts of the dataset simultaneously, without having to transfer individual files, which would make it very difficult to track which version is up-to-date.
162
+
Relational database management systems (RDBMS) also have the advantage that multiple users can access, store and analyse parts of the dataset simultaneously, without having to transfer individual files, which would make it very difficult to track which version is up-to-date.
159
163
160
164
:::::::::::::
161
165
162
-
The following code chunk demonstrates in four steps how to create a temporary SQLite database in memory, store the `ebola_confirmed` as a table on it, and subsequently read it:
166
+
The following code chunk demonstrates in four steps how to create a SQLite database in memory, store the `ebola_confirmed` as a table on it, and subsequently read it.
163
167
164
-
### 1. Connect with a database
168
+
### 1. Connection to the a database
165
169
166
-
First, we establish a connection to an SQLite database created on our machine and stored in its local memory with `DBI::dbConnect()`.
170
+
We first need to establish a connection to an SQLite database created on our machine and stored in its local memory with `DBI::dbConnect()`.
### 2. Write a local data frame as a table in a database
199
+
### 2. Create a table in the database from a data frame
196
200
197
-
Then, we can write the `ebola_confirmed` into a table named `cases` within the database using the `DBI::dbWriteTable()` function.
201
+
After establishing the connection with the database, we can now write out the `ebola_confirmed` data frame into a table named `cases` within the database using the `DBI::dbWriteTable()` function.
198
202
199
203
```{r,warning=FALSE,message=FALSE}
200
-
# Store the 'ebola_confirmed' dataframe as a table named 'cases'
204
+
# Store the 'ebola_confirmed' data frame as a table named 'cases'
201
205
# in the SQLite database
202
206
DBI::dbWriteTable(
203
207
conn = db_connection,
@@ -206,7 +210,7 @@ DBI::dbWriteTable(
206
210
)
207
211
```
208
212
209
-
In a database framework, you can have more than one table. Each table can belong to a specific `entity` (e.g., patients, care units, jobs). All tables will be related by a common ID or `primary key`.
213
+
In a relational database framework, you can have more than one table. Each table can belong to a specific `entity` (e.g., patients, care units, jobs). All tables will be related by a common ID or `primary key`.
210
214
211
215
### 3. Read data from a table in a database
212
216
@@ -220,17 +224,17 @@ In a database framework, you can have more than one table. Each table can belong
220
224
<!-- ) -->
221
225
<!-- ``` -->
222
226
223
-
Subsequently, we reads the data from the `cases` table using `dplyr::tbl()`.
227
+
We can reads the data from the `cases` table using `dplyr::tbl()`.
The `extracted_data` object represents the extracted, ideally after specifying queries that reduces its size.
256
+
The `extracted_data` object represents the extracted data, ideally after applying the specified queries that reduces its size.
253
257
254
258
```{r,warning=FALSE,message=FALSE}
255
259
# View the extracted_data
@@ -258,11 +262,11 @@ extracted_data
258
262
259
263
:::::::::::::::::::::: callout
260
264
261
-
### Run SQL queries in R using dbplyr
265
+
### Run SQL queries in R using {dbplyr}
262
266
263
-
Practice how to make relational database SQL queries using multiple `{dplyr}` verbs like `dplyr::left_join()` among tables before pulling down data to your local session with `dplyr::collect()`!
267
+
Practice how to make relational database SQL queries using multiple `{dplyr}` verbs like `dplyr::left_join()` among tables before pulling out data to your local session with `dplyr::collect()`!
264
268
265
-
You can also review the `{dbplyr}` R package. But for a step-by-step tutorial about SQL, we recommend you this [tutorial about data management with SQL for Ecologist](https://datacarpentry.org/sql-ecology-lesson/). You will find close to `{dplyr}`!
269
+
You can also review the `{dbplyr}` R package. But for a step-by-step tutorial about SQL, we recommend you this [tutorial about data management with SQL for Ecologist](https://datacarpentry.org/sql-ecology-lesson/).
Health related data are also increasingly stored in specialized HIS APIs like **Fingertips**, **GoData**, **REDCap**, and **DHIS2**. In such case one can resort to [readepi](https://epiverse-trace.github.io/readepi/) package, which enables reading data from HIS-APIs.
285
+
Health related data are also increasingly stored in specialized HIS APIs like **Fingertips**, **GoData**, **REDCap**, and **DHIS2**. In such case one can resort to the [{readepi}](https://epiverse-trace.github.io/readepi/) package, which enables reading data from HISAPIs.
282
286
-[TBC]
283
287
284
288
::::::::::::::::::::::::::::::::::::: keypoints
285
289
- Use `{rio}, {io}, {readr}` and `{ImportExport}` to read data from individual files.
290
+
- Use {DBI}, {dplyr}, and {dbplyr} to import data from RDBMS
286
291
- Use `{readepi}` to read data form HIS APIs and RDBMS.
0 commit comments