Skip to content

Commit

Permalink
Reading from databases
Browse files Browse the repository at this point in the history
  • Loading branch information
Degoot-AM committed Feb 16, 2024
1 parent a534352 commit 07b0a5c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions episodes/read-cases.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,33 @@ rio::import("/path_name/file_name.zip")

## Reading from databases

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.


The following code chunk demonstrates how to create a temporary SQLite database in memory, store the `case_data` dataframe as a table within it, and subsequently read from it:

```{r,warning=FALSE,message=FALSE}
library(DBI)
library(RSQLite)
# Create a temporary SQLite database in memory
db_con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
# Store the 'case_data' dataframe as a table named 'cases' in the SQLite database
DBI::dbWriteTable(db_con, "cases", case_data)
# Read data from the 'cases' table
result <- DBI::dbReadTable(db_con, "cases")
# Close the database connection
DBI::dbDisconnect(db_con)
# View the result
base::print(utils::head(result))
```

This code first establishes a connection to an SQLite database created in memory using `dbConnect` function. Then, it writes the `case_data` dataframe into a table named 'cases' within the database using `dbWriteTable` function. Subsequently, it reads the data from the 'cases' table using `dbReadTable` function. Finally, it closes the database connection with `dbDisconnect` function.

More examples about SQL databases and R can be found [here](https://datacarpentry.org/R-ecology-lesson/05-r-and-databases.html).

Check warning on line 95 in episodes/read-cases.Rmd

View workflow job for this annotation

GitHub Actions / Build Full Site

[uninformative link text]: [here](https://datacarpentry.org/R-ecology-lesson/05-r-and-databases.html)

## Reading from HIS APIs

Coming soon with {readepi}

::::::::::::::::::::::::::::::::::::: keypoints
- Use `{rio}, {io}, {readr}` and `{ImportExport}` to read data from individual files.
Expand Down
26 changes: 26 additions & 0 deletions renv/profiles/lesson-requirements/renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@
],
"Hash": "45f0398006e83a5b10b72a90663d8d8c"
},
"RSQLite": {
"Package": "RSQLite",
"Version": "2.3.5",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"DBI",
"R",
"bit64",
"blob",
"cpp11",
"memoise",
"methods",
"pkgconfig",
"plogr",
"rlang"
],
"Hash": "f5a75d57e0a3014a6ef537ac04a80fc6"
},
"Rcpp": {
"Package": "Rcpp",
"Version": "1.0.12",
Expand Down Expand Up @@ -1431,6 +1450,13 @@
],
"Hash": "01f28d4278f15c76cddbea05899c5d6f"
},
"plogr": {
"Package": "plogr",
"Version": "0.2.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "09eb987710984fc2905c7129c7d85e65"
},
"plyr": {
"Package": "plyr",
"Version": "1.8.9",
Expand Down

0 comments on commit 07b0a5c

Please sign in to comment.