forked from cienciadedatos/pt-r4ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheets.qmd
667 lines (499 loc) · 27.7 KB
/
spreadsheets.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# Spreadsheets {#sec-import-spreadsheets}
```{r}
#| echo: false
source("_common.R")
```
## Introduction
In @sec-data-import you learned about importing data from plain text files like `.csv` and `.tsv`.
Now it's time to learn how to get data out of a spreadsheet, either an Excel spreadsheet or a Google Sheet.
This will build on much of what you've learned in @sec-data-import, but we will also discuss additional considerations and complexities when working with data from spreadsheets.
If you or your collaborators are using spreadsheets for organizing data, we strongly recommend reading the paper "Data Organization in Spreadsheets" by Karl Broman and Kara Woo: <https://doi.org/10.1080/00031305.2017.1375989>.
The best practices presented in this paper will save you much headache when you import data from a spreadsheet into R to analyze and visualize.
## Excel
Microsoft Excel is a widely used spreadsheet software program where data are organized in worksheets inside of spreadsheet files.
### Prerequisites
In this section, you'll learn how to load data from Excel spreadsheets in R with the **readxl** package.
This package is non-core tidyverse, so you need to load it explicitly, but it is installed automatically when you install the tidyverse package.
Later, we'll also use the writexl package, which allows us to create Excel spreadsheets.
```{r}
#| message: false
library(readxl)
library(tidyverse)
library(writexl)
```
### Getting started
Most of readxl's functions allow you to load Excel spreadsheets into R:
- `read_xls()` reads Excel files with `xls` format.
- `read_xlsx()` read Excel files with `xlsx` format.
- `read_excel()` can read files with both `xls` and `xlsx` format. It guesses the file type based on the input.
These functions all have similar syntax just like other functions we have previously introduced for reading other types of files, e.g., `read_csv()`, `read_table()`, etc.
For the rest of the chapter we will focus on using `read_excel()`.
### Reading Excel spreadsheets {#sec-reading-spreadsheets-excel}
@fig-students-excel shows what the spreadsheet we're going to read into R looks like in Excel.
```{r}
#| label: fig-students-excel
#| echo: false
#| fig-width: 5
#| fig-cap: |
#| Spreadsheet called students.xlsx in Excel.
#| fig-alt: |
#| A look at the students spreadsheet in Excel. The spreadsheet contains
#| information on 6 students, their ID, full name, favourite food, meal plan,
#| and age.
knitr::include_graphics("screenshots/import-spreadsheets-students.png")
```
The first argument to `read_excel()` is the path to the file to read.
```{r}
students <- read_excel("data/students.xlsx")
```
`read_excel()` will read the file in as a tibble.
```{r}
students
```
We have six students in the data and five variables on each student.
However there are a few things we might want to address in this dataset:
1. The column names are all over the place.
You can provide column names that follow a consistent format; we recommend `snake_case` using the `col_names` argument.
```{r}
#| include: false
options(
dplyr.print_min = 7,
dplyr.print_max = 7
)
```
```{r}
read_excel(
"data/students.xlsx",
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age")
)
```
```{r}
#| include: false
options(
dplyr.print_min = 6,
dplyr.print_max = 6
)
```
Unfortunately, this didn't quite do the trick.
We now have the variable names we want, but what was previously the header row now shows up as the first observation in the data.
You can explicitly skip that row using the `skip` argument.
```{r}
read_excel(
"data/students.xlsx",
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age"),
skip = 1
)
```
2. In the `favourite_food` column, one of the observations is `N/A`, which stands for "not available" but it's currently not recognized as an `NA` (note the contrast between this `N/A` and the age of the fourth student in the list).
You can specify which character strings should be recognized as `NA`s with the `na` argument.
By default, only `""` (empty string, or, in the case of reading from a spreadsheet, an empty cell or a cell with the formula `=NA()`) is recognized as an `NA`.
```{r}
read_excel(
"data/students.xlsx",
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age"),
skip = 1,
na = c("", "N/A")
)
```
3. One other remaining issue is that `age` is read in as a character variable, but it really should be numeric.
Just like with `read_csv()` and friends for reading data from flat files, you can supply a `col_types` argument to `read_excel()` and specify the column types for the variables you read in.
The syntax is a bit different, though.
Your options are `"skip"`, `"guess"`, `"logical"`, `"numeric"`, `"date"`, `"text"` or `"list"`.
```{r}
read_excel(
"data/students.xlsx",
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age"),
skip = 1,
na = c("", "N/A"),
col_types = c("numeric", "text", "text", "text", "numeric")
)
```
However, this didn't quite produce the desired result either.
By specifying that `age` should be numeric, we have turned the one cell with the non-numeric entry (which had the value `five`) into an `NA`.
In this case, we should read age in as `"text"` and then make the change once the data is loaded in R.
```{r}
students <- read_excel(
"data/students.xlsx",
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age"),
skip = 1,
na = c("", "N/A"),
col_types = c("numeric", "text", "text", "text", "text")
)
students <- students |>
mutate(
age = if_else(age == "five", "5", age),
age = parse_number(age)
)
students
```
It took us multiple steps and trial-and-error to load the data in exactly the format we want, and this is not unexpected.
Data science is an iterative process, and the process of iteration can be even more tedious when reading data in from spreadsheets compared to other plain text, rectangular data files because humans tend to input data into spreadsheets and use them not just for data storage but also for sharing and communication.
There is no way to know exactly what the data will look like until you load it and take a look at it.
Well, there is one way, actually.
You can open the file in Excel and take a peek.
If you're going to do so, we recommend making a copy of the Excel file to open and browse interactively while leaving the original data file untouched and reading into R from the untouched file.
This will ensure you don't accidentally overwrite anything in the spreadsheet while inspecting it.
You should also not be afraid of doing what we did here: load the data, take a peek, make adjustments to your code, load it again, and repeat until you're happy with the result.
### Reading worksheets
An important feature that distinguishes spreadsheets from flat files is the notion of multiple sheets, called worksheets.
@fig-penguins-islands shows an Excel spreadsheet with multiple worksheets.
The data come from the **palmerpenguins** package.
Each worksheet contains information on penguins from a different island where data were collected.
```{r}
#| label: fig-penguins-islands
#| echo: false
#| fig-cap: |
#| Spreadsheet called penguins.xlsx in Excel containing three worksheets.
#| fig-alt: |
#| A look at the penguins spreadsheet in Excel. The spreadsheet contains has
#| three worksheets: Torgersen Island, Biscoe Island, and Dream Island.
knitr::include_graphics("screenshots/import-spreadsheets-penguins-islands.png")
```
You can read a single worksheet from a spreadsheet with the `sheet` argument in `read_excel()`.
The default, which we've been relying on up until now, is the first sheet.
```{r}
read_excel("data/penguins.xlsx", sheet = "Torgersen Island")
```
Some variables that appear to contain numerical data are read in as characters due to the character string `"NA"` not being recognized as a true `NA`.
```{r}
penguins_torgersen <- read_excel("data/penguins.xlsx", sheet = "Torgersen Island", na = "NA")
penguins_torgersen
```
Alternatively, you can use `excel_sheets()` to get information on all worksheets in an Excel spreadsheet, and then read the one(s) you're interested in.
```{r}
excel_sheets("data/penguins.xlsx")
```
Once you know the names of the worksheets, you can read them in individually with `read_excel()`.
```{r}
penguins_biscoe <- read_excel("data/penguins.xlsx", sheet = "Biscoe Island", na = "NA")
penguins_dream <- read_excel("data/penguins.xlsx", sheet = "Dream Island", na = "NA")
```
In this case the full penguins dataset is spread across three worksheets in the spreadsheet.
Each worksheet has the same number of columns but different numbers of rows.
```{r}
dim(penguins_torgersen)
dim(penguins_biscoe)
dim(penguins_dream)
```
We can put them together with `bind_rows()`.
```{r}
penguins <- bind_rows(penguins_torgersen, penguins_biscoe, penguins_dream)
penguins
```
In @sec-iteration we'll talk about ways of doing this sort of task without repetitive code.
### Reading part of a sheet
Since many use Excel spreadsheets for presentation as well as for data storage, it's quite common to find cell entries in a spreadsheet that are not part of the data you want to read into R.
@fig-deaths-excel shows such a spreadsheet: in the middle of the sheet is what looks like a data frame but there is extraneous text in cells above and below the data.
```{r}
#| label: fig-deaths-excel
#| echo: false
#| fig-cap: |
#| Spreadsheet called deaths.xlsx in Excel.
#| fig-alt: |
#| A look at the deaths spreadsheet in Excel. The spreadsheet has four rows
#| on top that contain non-data information; the text 'For the same of
#| consistency in the data layout, which is really a beautiful thing, I will
#| keep making notes up here.' is spread across cells in these top four rows.
#| Then, there is a data frame that includes information on deaths of 10
#| famous people, including their names, professions, ages, whether they have
#| kids or not, date of birth and death. At the bottom, there are four more
#| rows of non-data information; the text 'This has been really fun, but
#| we're signing off now!' is spread across cells in these bottom four rows.
knitr::include_graphics("screenshots/import-spreadsheets-deaths.png")
```
This spreadsheet is one of the example spreadsheets provided in the readxl package.
You can use the `readxl_example()` function to locate the spreadsheet on your system in the directory where the package is installed.
This function returns the path to the spreadsheet, which you can use in `read_excel()` as usual.
```{r}
deaths_path <- readxl_example("deaths.xlsx")
deaths <- read_excel(deaths_path)
deaths
```
The top three rows and the bottom four rows are not part of the data frame.
It's possible to eliminate these extraneous rows using the `skip` and `n_max` arguments, but we recommend using cell ranges.
In Excel, the top left cell is `A1`.
As you move across columns to the right, the cell label moves down the alphabet, i.e.
`B1`, `C1`, etc.
And as you move down a column, the number in the cell label increases, i.e.
`A2`, `A3`, etc.
Here the data we want to read in starts in cell `A5` and ends in cell `F15`.
In spreadsheet notation, this is `A5:F15`, which we supply to the `range` argument:
```{r}
read_excel(deaths_path, range = "A5:F15")
```
### Data types
In CSV files, all values are strings.
This is not particularly true to the data, but it is simple: everything is a string.
The underlying data in Excel spreadsheets is more complex.
A cell can be one of four things:
- A boolean, like `TRUE`, `FALSE`, or `NA`.
- A number, like "10" or "10.5".
- A datetime, which can also include time like "11/1/21" or "11/1/21 3:00 PM".
- A text string, like "ten".
When working with spreadsheet data, it's important to keep in mind that the underlying data can be very different than what you see in the cell.
For example, Excel has no notion of an integer.
All numbers are stored as floating points, but you can choose to display the data with a customizable number of decimal points.
Similarly, dates are actually stored as numbers, specifically the number of seconds since January 1, 1970.
You can customize how you display the date by applying formatting in Excel.
Confusingly, it's also possible to have something that looks like a number but is actually a string (e.g., type `'10` into a cell in Excel).
These differences between how the underlying data are stored vs. how they're displayed can cause surprises when the data are loaded into R.
By default readxl will guess the data type in a given column.
A recommended workflow is to let readxl guess the column types, confirm that you're happy with the guessed column types, and if not, go back and re-import specifying `col_types` as shown in @sec-reading-spreadsheets-excel.
Another challenge is when you have a column in your Excel spreadsheet that has a mix of these types, e.g., some cells are numeric, others text, others dates.
When importing the data into R readxl has to make some decisions.
In these cases you can set the type for this column to `"list"`, which will load the column as a list of length 1 vectors, where the type of each element of the vector is guessed.
::: callout-note
Sometimes data is stored in more exotic ways, like the color of the cell background, or whether or not the text is bold.
In such cases, you might find the [tidyxl package](https://nacnudus.github.io/tidyxl/) useful.
See <https://nacnudus.github.io/spreadsheet-munging-strategies/> for more on strategies for working with non-tabular data from Excel.
:::
### Writing to Excel {#sec-writing-to-excel}
Let's create a small data frame that we can then write out.
Note that `item` is a factor and `quantity` is an integer.
```{r}
bake_sale <- tibble(
item = factor(c("brownie", "cupcake", "cookie")),
quantity = c(10, 5, 8)
)
bake_sale
```
You can write data back to disk as an Excel file using the `write_xlsx()` from the [writexl package](https://docs.ropensci.org/writexl/):
```{r}
#| eval: false
write_xlsx(bake_sale, path = "data/bake-sale.xlsx")
```
@fig-bake-sale-excel shows what the data looks like in Excel.
Note that column names are included and bolded.
These can be turned off by setting `col_names` and `format_headers` arguments to `FALSE`.
```{r}
#| label: fig-bake-sale-excel
#| echo: false
#| fig-width: 5
#| fig-cap: |
#| Spreadsheet called bake_sale.xlsx in Excel.
#| fig-alt: |
#| Bake sale data frame created earlier in Excel.
knitr::include_graphics("screenshots/import-spreadsheets-bake-sale.png")
```
Just like reading from a CSV, information on data type is lost when we read the data back in.
This makes Excel files unreliable for caching interim results as well.
For alternatives, see @sec-writing-to-a-file.
```{r}
read_excel("data/bake-sale.xlsx")
```
### Formatted output
The writexl package is a light-weight solution for writing a simple Excel spreadsheet, but if you're interested in additional features like writing to sheets within a spreadsheet and styling, you will want to use the [openxlsx package](https://ycphs.github.io/openxlsx).
We won't go into the details of using this package here, but we recommend reading <https://ycphs.github.io/openxlsx/articles/Formatting.html> for an extensive discussion on further formatting functionality for data written from R to Excel with openxlsx.
Note that this package is not part of the tidyverse so the functions and workflows may feel unfamiliar.
For example, function names are camelCase, multiple functions can't be composed in pipelines, and arguments are in a different order than they tend to be in the tidyverse.
However, this is ok.
As your R learning and usage expands outside of this book you will encounter lots of different styles used in various R packages that you might use to accomplish specific goals in R.
A good way of familiarizing yourself with the coding style used in a new package is to run the examples provided in function documentation to get a feel for the syntax and the output formats as well as reading any vignettes that might come with the package.
### Exercises
1. In an Excel file, create the following dataset and save it as `survey.xlsx`.
Alternatively, you can download it as an Excel file from [here](https://docs.google.com/spreadsheets/d/1yc5gL-a2OOBr8M7B3IsDNX5uR17vBHOyWZq6xSTG2G8).
```{r}
#| echo: false
#| fig-width: 4
#| fig-alt: |
#| A spreadsheet with 3 columns (group, subgroup, and id) and 12 rows.
#| The group column has two values: 1 (spanning 7 merged rows) and 2
#| (spanning 5 merged rows). The subgroup column has four values: A
#| (spanning 3 merged rows), B (spanning 4 merged rows), A (spanning 2
#| merged rows), and B (spanning 3 merged rows). The id column has twelve
#| values, numbers 1 through 12.
knitr::include_graphics("screenshots/import-spreadsheets-survey.png")
```
Then, read it into R, with `survey_id` as a character variable and `n_pets` as a numerical variable.
```{r}
#| echo: false
read_excel("data/survey.xlsx", na = c("", "N/A"), col_types = c("text", "text")) |>
mutate(
n_pets = case_when(
n_pets == "none" ~ "0",
n_pets == "two" ~ "2",
TRUE ~ n_pets
),
n_pets = as.numeric(n_pets)
)
```
2. In another Excel file, create the following dataset and save it as `roster.xlsx`.
Alternatively, you can download it as an Excel file from [here](https://docs.google.com/spreadsheets/d/1LgZ0Bkg9d_NK8uTdP2uHXm07kAlwx8-Ictf8NocebIE).
```{r}
#| echo: false
#| fig-width: 4
#| fig-alt: |
#| A spreadsheet with 3 columns (group, subgroup, and id) and 12 rows. The
#| group column has two values: 1 (spanning 7 merged rows) and 2 (spanning
#| 5 merged rows). The subgroup column has four values: A (spanning 3 merged
#| rows), B (spanning 4 merged rows), A (spanning 2 merged rows), and B
#| (spanning 3 merged rows). The id column has twelve values, numbers 1
#| through 12.
knitr::include_graphics("screenshots/import-spreadsheets-roster.png")
```
Then, read it into R.
The resulting data frame should be called `roster` and should look like the following.
```{r}
#| echo: false
#| message: false
read_excel("data/roster.xlsx") |>
fill(group, subgroup) |>
print(n = 12)
```
3. In a new Excel file, create the following dataset and save it as `sales.xlsx`.
Alternatively, you can download it as an Excel file from [here](https://docs.google.com/spreadsheets/d/1oCqdXUNO8JR3Pca8fHfiz_WXWxMuZAp3YiYFaKze5V0).
```{r}
#| echo: false
#| fig-alt: |
#| A spreadsheet with 2 columns and 13 rows. The first two rows have text
#| containing information about the sheet. Row 1 says "This file contains
#| information on sales". Row 2 says "Data are organized by brand name, and
#| for each brand, we have the ID number for the item sold, and how many are
#| sold.". Then there are two empty rows, and then 9 rows of data.
knitr::include_graphics("screenshots/import-spreadsheets-sales.png")
```
a\.
Read `sales.xlsx` in and save as `sales`.
The data frame should look like the following, with `id` and `n` as column names and with 9 rows.
```{r}
#| echo: false
#| message: false
read_excel("data/sales.xlsx", skip = 3, col_names = c("id", "n")) |>
print(n = 9)
```
b\.
Modify `sales` further to get it into the following tidy format with three columns (`brand`, `id`, and `n`) and 7 rows of data.
Note that `id` and `n` are numeric, `brand` is a character variable.
```{r}
#| echo: false
#| message: false
read_excel("data/sales.xlsx", skip = 3, col_names = c("id", "n")) |>
mutate(brand = if_else(str_detect(id, "Brand"), id, NA)) |>
fill(brand) |>
filter(n != "n") |>
relocate(brand) |>
mutate(
id = as.numeric(id),
n = as.numeric(n)
) |>
print(n = 7)
```
4. Recreate the `bake_sale` data frame, write it out to an Excel file using the `write.xlsx()` function from the openxlsx package.
5. In @sec-data-import you learned about the `janitor::clean_names()` function to turn columns names into snake case.
Read the `students.xlsx` file that we introduced earlier in this section and use this function to "clean" the column names.
6. What happens if you try to read in a file with `.xlsx` extension with `read_xls()`?
## Google Sheets
Google Sheets is another widely used spreadsheet program.
It's free and web-based.
Just like with Excel, in Google Sheets data are organized in worksheets (also called sheets) inside of spreadsheet files.
### Prerequisites
This section will also focus on spreadsheets, but this time you'll be loading data from a Google Sheet with the **googlesheets4** package.
This package is non-core tidyverse as well, you need to load it explicitly.
```{r}
library(googlesheets4)
library(tidyverse)
```
A quick note about the name of the package: googlesheets4 uses v4 of the [Sheets API v4](https://developers.google.com/sheets/api/) to provide an R interface to Google Sheets, hence the name.
### Getting started
The main function of the googlesheets4 package is `read_sheet()`, which reads a Google Sheet from a URL or a file id.
This function also goes by the name `range_read()`.
You can also create a brand new sheet with `gs4_create()` or write to an existing sheet with `sheet_write()` and friends.
In this section we'll work with the same datasets as the ones in the Excel section to highlight similarities and differences between workflows for reading data from Excel and Google Sheets.
readxl and googlesheets4 packages are both designed to mimic the functionality of the readr package, which provides the `read_csv()` function you've seen in @sec-data-import.
Therefore, many of the tasks can be accomplished with simply swapping out `read_excel()` for `read_sheet()`.
However you'll also see that Excel and Google Sheets don't behave in exactly the same way, therefore other tasks may require further updates to the function calls.
### Reading Google Sheets
@fig-students-googlesheets shows what the spreadsheet we're going to read into R looks like in Google Sheets.
This is the same dataset as in @fig-students-excel, except it's stored in a Google Sheet instead of Excel.
```{r}
#| label: fig-students-googlesheets
#| echo: false
#| fig-cap: |
#| Google Sheet called students in a browser window.
#| fig-alt: |
#| A look at the students spreadsheet in Google Sheets. The spreadsheet contains
#| information on 6 students, their ID, full name, favourite food, meal plan,
#| and age.
knitr::include_graphics("screenshots/import-googlesheets-students.png")
```
The first argument to `read_sheet()` is the URL of the file to read, and it returns a tibble:\
<https://docs.google.com/spreadsheets/d/1V1nPp1tzOuutXFLb3G9Eyxi3qxeEhnOXUzL5_BcCQ0w>.
These URLs are not pleasant to work with, so you'll often want to identify a sheet by its ID.
```{r}
#| include: false
gs4_deauth()
```
```{r}
students_sheet_id <- "1V1nPp1tzOuutXFLb3G9Eyxi3qxeEhnOXUzL5_BcCQ0w"
students <- read_sheet(students_sheet_id)
students
```
Just like we did with `read_excel()`, we can supply column names, NA strings, and column types to `read_sheet()`.
```{r}
students <- read_sheet(
students_sheet_id,
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age"),
skip = 1,
na = c("", "N/A"),
col_types = "dcccc"
)
students
```
Note that we defined column types a bit differently here, using short codes.
For example, "dcccc" stands for "double, character, character, character, character".
It's also possible to read individual sheets from Google Sheets as well.
Let's read the "Torgersen Island" sheet from the [penguins Google Sheet](https://pos.it/r4ds-penguins):
```{r}
penguins_sheet_id <- "1aFu8lnD_g0yjF5O-K6SFgSEWiHPpgvFCF0NY9D6LXnY"
read_sheet(penguins_sheet_id, sheet = "Torgersen Island")
```
You can obtain a list of all sheets within a Google Sheet with `sheet_names()`:
```{r}
sheet_names(penguins_sheet_id)
```
Finally, just like with `read_excel()`, we can read in a portion of a Google Sheet by defining a `range` in `read_sheet()`.
Note that we're also using the `gs4_example()` function below to locate an example Google Sheet that comes with the googlesheets4 package.
```{r}
deaths_url <- gs4_example("deaths")
deaths <- read_sheet(deaths_url, range = "A5:F15")
deaths
```
### Writing to Google Sheets
You can write from R to Google Sheets with `write_sheet()`.
The first argument is the data frame to write, and the second argument is the name (or other identifier) of the Google Sheet to write to:
```{r}
#| eval: false
write_sheet(bake_sale, ss = "bake-sale")
```
If you'd like to write your data to a specific (work)sheet inside a Google Sheet, you can specify that with the `sheet` argument as well.
```{r}
#| eval: false
write_sheet(bake_sale, ss = "bake-sale", sheet = "Sales")
```
### Authentication
While you can read from a public Google Sheet without authenticating with your Google account, reading a private sheet or writing to a sheet requires authentication so that googlesheets4 can view and manage *your* Google Sheets.
When you attempt to read in a sheet that requires authentication, googlesheets4 will direct you to a web browser with a prompt to sign in to your Google account and grant permission to operate on your behalf with Google Sheets.
However, if you want to specify a specific Google account, authentication scope, etc. you can do so with `gs4_auth()`, e.g., `gs4_auth(email = "[email protected]")`, which will force the use of a token associated with a specific email.
For further authentication details, we recommend reading the documentation googlesheets4 auth vignette: <https://googlesheets4.tidyverse.org/articles/auth.html>.
### Exercises
1. Read the `students` dataset from earlier in the chapter from Excel and also from Google Sheets, with no additional arguments supplied to the `read_excel()` and `read_sheet()` functions.
Are the resulting data frames in R exactly the same?
If not, how are they different?
2. Read the Google Sheet titled survey from <https://pos.it/r4ds-survey>, with `survey_id` as a character variable and `n_pets` as a numerical variable.
3. Read the Google Sheet titled roster from <https://pos.it/r4ds-roster>.
The resulting data frame should be called `roster` and should look like the following.
```{r}
#| echo: false
#| message: false
read_sheet("https://docs.google.com/spreadsheets/d/1LgZ0Bkg9d_NK8uTdP2uHXm07kAlwx8-Ictf8NocebIE/") |>
fill(group, subgroup) |>
print(n = 12)
```
## Summary
Microsoft Excel and Google Sheets are two of the most popular spreadsheet systems.
Being able to interact with data stored in Excel and Google Sheets files directly from R is a superpower!
In this chapter you learned how to read data into R from spreadsheets from Excel with `read_excel()` from the readxl package and from Google Sheets with `read_sheet()` from the googlesheets4 package.
These functions work very similarly to each other and have similar arguments for specifying column names, NA strings, rows to skip on top of the file you're reading in, etc.
Additionally, both functions make it possible to read a single sheet from a spreadsheet as well.
On the other hand, writing to an Excel file requires a different package and function (`writexl::write_xlsx()`) while you can write to a Google Sheet with the googlesheets4 package, with `write_sheet()`.
In the next chapter, you'll learn about a different data source and how to read data from that source into R: databases.