Skip to content

Commit 2258317

Browse files
committed
improving readability
1 parent ae71c97 commit 2258317

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

hands-on.qmd

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ species_db <- tbl(conn, "Species")
157157
species_db
158158
```
159159

160+
You can filter the data and select columns:
161+
160162
```{r}
161163
species_db %>%
162164
filter(Relevance=="Study species") %>%
@@ -165,9 +167,12 @@ species_db %>%
165167
head(3)
166168
```
167169

168-
Note that those are not data frames but tables. What `dbplyr` is actually doing behind the scenes is translating all those dplyr operations into SQL, sending the SQL code to query the database, retrieving results, etc.
170+
:::{.callout-note}
171+
## Note
172+
Note that those are **not** data frames but tables. What `dbplyr` is actually doing behind the scenes is translating all those dplyr operations into SQL, sending the SQL code to query the database, retrieving results, etc.
173+
:::
169174

170-
#### How can I get a "real data frame?"
175+
#### How can I get a "real" data frame?
171176

172177
You add `collect()` to your query.
173178

@@ -180,9 +185,11 @@ species_db %>%
180185
collect()
181186
```
182187

183-
Note it means the full query is going to be ran and save in your environment. This might slow things down so you generally want to collect on the smallest data frame you can
188+
Note it means the full query is going to be ran and save in your R environment. This might slow things down, so you generally want to collect on the smallest data frame you can.
189+
190+
#### How can you see the SQL query?
184191

185-
#### How can you see the SQL query equivalent to the tidyverse code? => `show_query()`
192+
Adding `show_query()` at the end of your code block will let you see the SQL code that has been used to query the database.
186193

187194
```{r}
188195
# Add show_query() to the end to see what SQL it is sending!
@@ -203,6 +210,7 @@ Here is how you could run the query using the SQL code directly:
203210
dbGetQuery(conn, "SELECT Scientific_name FROM Species WHERE (Relevance = 'Study species') ORDER BY Scientific_name LIMIT 3")
204211
```
205212

213+
206214
You can do pretty much anything with these quasi-tables, including grouping, summarization, joins, etc.
207215

208216
Let's count how many species there are per Relevance categories:
@@ -221,13 +229,15 @@ species_db %>%
221229
summarize(num_species = n()) %>%
222230
show_query()
223231
```
232+
224233
You can also create new columns using mutate:
225234

226235
```{r}
227236
species_db %>%
228237
mutate(Code = paste("X", Code)) %>%
229238
head()
230239
```
240+
231241
How does the query looks like?
232242

233243
```{r}
@@ -236,21 +246,22 @@ species_db %>%
236246
head() %>%
237247
show_query()
238248
```
249+
239250
:::{.callout-caution}
240-
****Limitation: no way to add or update data in the database, `dbplyr` is view only. If you want to add or update data, you'll need to use the `DBI` package functions.***
251+
***Limitation: no way to add or update data in the database, `dbplyr` is view only. If you want to add or update data, you'll need to use the `DBI` package functions.***
241252
:::
242253

243254
### Average egg volume analysis
244255

245-
Let's reproduce the egg volume analysis we just did. We can calculate the average bird eggs volume per species directly on the database
256+
Let's reproduce the egg volume analysis we just did. We can calculate the average bird eggs volume per species directly on the database:
246257

247258
```{r}
248259
# loading all the necessary tables
249260
eggs_db <- tbl(conn, "Bird_eggs")
250261
nests_db <- tbl(conn, "Bird_nests")
251262
```
252263

253-
Compute the volume:
264+
Compute the volume using the same code as previously!!
254265

255266
```{r}
256267
# Compute the egg volume
@@ -272,7 +283,7 @@ species_egg_volume_avg_db <- left_join(nests_db, eggs_area_db, by="Nest_ID") %>%
272283
species_egg_volume_avg_db
273284
```
274285

275-
What does this SQL quert looks like?
286+
What does this SQL query looks like?
276287

277288
```{r}
278289
species_egg_volume_avg_db <- left_join(nests_db, eggs_area_db, by="Nest_ID") %>%

0 commit comments

Comments
 (0)