<p>So far, you have learned about importing data from plain text files, e.g., <code>.csv</code> and <code>.tsv</code> files. Sometimes you need to analyze data that lives in a spreadsheet. This chapter will introduce you to tools for working with data in Excel spreadsheets and Google Sheets. This will build on much of what you’ve learned in <ahref="#chp-data-import"data-type="xref">#chp-data-import</a>, but we will also discuss additional considerations and complexities when working with data from spreadsheets.</p>
<p>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: <ahref="https://doi.org/10.1080/00031305.2017.1375989"class="uri">https://doi.org/10.1080/00031305.2017.1375989</a>. 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.</p>
<p>In this section, you’ll learn how to load data from Excel spreadsheets in R with the <strong>readxl</strong> 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.</p>
<p><strong>openxlsx</strong>, <strong>xlsx</strong>, and <strong>XLConnect</strong> can also be used for reading data from and writing data to Excel spreadsheets. We will discuss openxlsx in <ahref="#sec-writing-to-excel"data-type="xref">#sec-writing-to-excel</a>. The latter two packages require Java installed on your machine and the rJava package. Due to potential challenges with installation, we recommend using alternative packages we’re introducing in this chapter.</p>
<code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> can read files with both <code>xls</code> and <code>xlsx</code> format. It guesses the file type based on the input.</li>
</ul><p>These functions all have similar syntax just like other functions we have previously introduced for reading other types of files, e.g.<code><ahref="https://readr.tidyverse.org/reference/read_delim.html">read_csv()</a></code>, <code><ahref="https://readr.tidyverse.org/reference/read_table.html">read_table()</a></code>, etc. For the rest of the chapter we will focus on using <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<figureid="fig-students-excel"><p><imgsrc="screenshots/import-spreadsheets-students.png"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."width="1200"/></p>
<p>The first argument to <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> is the path to the file to read.</p>
#> 1 1 Sunil Huffmann Strawberry yoghurt Lunch only 4
#> 2 2 Barclay Lynn French fries Lunch only 5
#> 3 3 Jayendra Lyne N/A Breakfast and lunch 7
#> 4 4 Leon Rossini Anchovies Lunch only <NA>
#> 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch five
#> 6 6 Güvenç Attila Ice cream Lunch only 6</pre>
</div>
<p>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:</p>
<oltype="1"><li>
<p>The column names are all over the place. You can provide column names that follow a consistent format; we recommend <code>snake_case</code> using the <code>col_names</code> argument.</p>
<p>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 <code>skip</code> argument.</p>
<p>In the <code>favourite_food</code> column, one of the observations is <code>N/A</code>, which stands for “not available” but it’s currently not recognized as an <code>NA</code> (note the contrast between this <code>N/A</code> and the age of the fourth student in the list). You can specify which character strings should be recognized as <code>NA</code>s with the <code>na</code> argument. By default, only <code>""</code> (empty string, or, in the case of reading from a spreadsheet, an empty cell or a cell with the formula <code>=NA()</code>) is recognized as an <code>NA</code>.</p>
<p>One other remaining issue is that <code>age</code> is read in as a character variable, but it really should be numeric. Just like with <code><ahref="https://readr.tidyverse.org/reference/read_delim.html">read_csv()</a></code> and friends for reading data from flat files, you can supply a <code>col_types</code> argument to <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> and specify the column types for the variables you read in. The syntax is a bit different, though. Your options are <code>"skip"</code>, <code>"guess"</code>, <code>"logical"</code>, <code>"numeric"</code>, <code>"date"</code>, <code>"text"</code> or <code>"list"</code>.</p>
#> 1 1 Sunil Huffmann Strawberry yoghurt Lunch only 4
#> 2 2 Barclay Lynn French fries Lunch only 5
#> 3 3 Jayendra Lyne <NA> Breakfast and lunch 7
#> 4 4 Leon Rossini Anchovies Lunch only NA
#> 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch NA
#> 6 6 Güvenç Attila Ice cream Lunch only 6</pre>
</div>
<p>However, this didn’t quite produce the desired result either. By specifying that <code>age</code> should be numeric, we have turned the one cell with the non-numeric entry (which had the value <code>five</code>) into an <code>NA</code>. In this case, we should read age in as <code>"text"</code> and then make the change once the data is loaded in R.</p>
</ol><p>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.</p>
<p>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.</p>
<p>An important feature that distinguishes spreadsheets from flat files is the notion of multiple sheets, called worksheets. <ahref="#fig-penguins-islands"data-type="xref">#fig-penguins-islands</a> shows an Excel spreadsheet with multiple worksheets. The data come from the <strong>palmerpenguins</strong> package. Each worksheet contains information on penguins from a different island where data were collected.</p>
<figureid="fig-penguins-islands"><p><imgsrc="screenshots/import-spreadsheets-penguins-islands.png"alt="A look at the penguins spreadsheet in Excel. The spreadsheet contains has three worksheets: Torgersen Island, Biscoe Island, and Dream Island."width="1514"/></p>
<figcaption>Spreadsheet called penguins.xlsx in Excel containing three worksheets.</figcaption>
<p>You can read a single worksheet from a spreadsheet with the <code>sheet</code> argument in <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<p>Some variables that appear to contain numerical data are read in as characters due to the character string <code>"NA"</code> not being recognized as a true <code>NA</code>.</p>
<p>Alternatively, you can use <code><ahref="https://readxl.tidyverse.org/reference/excel_sheets.html">excel_sheets()</a></code> to get information on all worksheets in an Excel spreadsheet, and then read the one(s) you’re interested in.</p>
<p>Once you know the names of the worksheets, you can read them in individually with <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<p>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.</p>
<p>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. <ahref="#fig-deaths-excel"data-type="xref">#fig-deaths-excel</a> 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.</p>
<figureid="fig-deaths-excel"><p><imgsrc="screenshots/import-spreadsheets-deaths.png"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."width="1614"/></p>
<p>This spreadsheet is one of the example spreadsheets provided in the readxl package. You can use the <code><ahref="https://readxl.tidyverse.org/reference/readxl_example.html">readxl_example()</a></code> 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 <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> as usual.</p>
<p>The top three rows and the bottom four rows are not part of the data frame.</p>
<p>We could skip the top three rows with <code>skip</code>. Note that we set <code>skip = 4</code> since the fourth row contains column names, not the data.</p>
<p>Another approach is using cell ranges. In Excel, the top left cell is <code>A1</code>. As you move across columns to the right, the cell label moves down the alphabet, i.e. <code>B1</code>, <code>C1</code>, etc. And as you move down a column, the number in the cell label increases, i.e. <code>A2</code>, <code>A3</code>, etc.</p>
<p>The data we want to read in starts in cell <code>A5</code> and ends in cell <code>F15</code>. In spreadsheet notation, this is <code>A5:F15</code>.</p>
<ul><li>
<p>Supply this information to the <code>range</code> argument:</p>
</ul><p>When working with spreadsheet data, it’s important to keep in mind that how the underlying data is stored 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 <code>'10</code> into a cell in Excel).</p>
<p>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 <code>col_types</code> as shown in <ahref="#sec-reading-spreadsheets"data-type="xref">#sec-reading-spreadsheets</a>.</p>
<p>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 <code>"list"</code>, which will load the column as a list of length 1 vectors, where the type of each element of the vector is guessed.</p>
<p><strong>tidyxl</strong> is useful for importing non-tabular data from Excel files into R. For example, tidyxl doesn’t coerce a pivot table into a data frame. See <ahref="https://nacnudus.github.io/spreadsheet-munging-strategies/"class="uri">https://nacnudus.github.io/spreadsheet-munging-strategies/</a> for more on strategies for working with non-tabular data from Excel.</p>
<p>You can write data back to disk as an Excel file using the <code><ahref="https://docs.ropensci.org/writexl/reference/write_xlsx.html">write_xlsx()</a></code> from the <strong>writexl</strong> package.</p>
<p><ahref="#fig-bake-sale-excel"data-type="xref">#fig-bake-sale-excel</a> shows what the data looks like in Excel. Note that column names are included and bolded. These can be turned off by setting <code>col_names</code> and <code>format_headers</code> arguments to <code>FALSE</code>.</p>
<figureid="fig-bake-sale-excel"><p><imgsrc="screenshots/import-spreadsheets-bake-sale.png"alt="Bake sale data frame created earlier in Excel."width="917"/></p>
<p>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 <ahref="#sec-writing-to-a-file"data-type="xref">#sec-writing-to-a-file</a>.</p>
<p>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 <strong>openxlsx</strong> package. 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 need to 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.</p>
<p>The resulting spreadsheet is shown in <ahref="#fig-penguins-species"data-type="xref">#fig-penguins-species</a>. By default, openxlsx formats the data as an Excel table.</p>
<figureid="fig-penguins-species"><p><imgsrc="screenshots/import-spreadsheets-penguins-species.png"alt="A look at the penguins spreadsheet in Excel. The spreadsheet contains has three sheets: Torgersen Island, Biscoe Island, and Dream Island."width="1106"/></p>
<p>See <ahref="https://ycphs.github.io/openxlsx/articles/Formatting.html"class="uri">https://ycphs.github.io/openxlsx/articles/Formatting.html</a> for an extensive discussion on further formatting functionality for data written from R to Excel with openxlsx.</p>
<p>In an Excel file, create the following dataset and save it as <code>survey.xlsx</code>. Alternatively, you can download it as an Excel file from <ahref="https://docs.google.com/spreadsheets/d/1yc5gL-a2OOBr8M7B3IsDNX5uR17vBHOyWZq6xSTG2G8/edit?usp=sharing">here</a>.</p>
<divclass="cell">
<divclass="cell-output-display">
<p><imgsrc="screenshots/import-spreadsheets-survey.png"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."width="263"/></p>
</div>
</div>
<p>Then, read it into R, with <code>survey_id</code> as a character variable and <code>n_pets</code> as a numerical variable. Hint: You will need to convert “none” to 0.</p>
<divclass="cell">
<pre><code>#> # A tibble: 6 × 2
#> survey_id n_pets
#><dbl><dbl>
#> 1 1 0
#> 2 2 1
#> 3 3 NA
#> 4 4 2
#> 5 5 2
#> 6 6 NA</code></pre>
</div>
</li>
<li>
<p>In another Excel file, create the following dataset and save it as <code>roster.xlsx</code>. Alternatively, you can download it as an Excel file from <ahref="https://docs.google.com/spreadsheets/d/1LgZ0Bkg9d_NK8uTdP2uHXm07kAlwx8-Ictf8NocebIE/edit?usp=sharing">here</a>.</p>
<divclass="cell">
<divclass="cell-output-display">
<p><imgsrc="screenshots/import-spreadsheets-roster.png"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."width="255"/></p>
</div>
</div>
<p>Then, read it into R. The resulting data frame should be called <code>roster</code> and should look like the following.</p>
<divclass="cell">
<pre><code>#> # A tibble: 12 × 3
#> group subgroup id
#><dbl><chr><dbl>
#> 1 1 A 1
#> 2 1 A 2
#> 3 1 A 3
#> 4 1 B 4
#> 5 1 B 5
#> 6 1 B 6
#> 7 1 B 7
#> 8 2 A 8
#> 9 2 A 9
#> 10 2 B 10
#> 11 2 B 11
#> 12 2 B 12</code></pre>
</div>
</li>
<li>
<p>In a new Excel file, create the following dataset and save it as <code>sales.xlsx</code>. Alternatively, you can download it as an Excel file from <ahref="https://docs.google.com/spreadsheets/d/1oCqdXUNO8JR3Pca8fHfiz_WXWxMuZAp3YiYFaKze5V0/edit?usp=sharing">here</a>.</p>
<divclass="cell">
<divclass="cell-output-display">
<p><imgsrc="screenshots/import-spreadsheets-sales.png"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."width="317"/></p>
</div>
</div>
<p>a. Read <code>sales.xlsx</code> in and save as <code>sales</code>. The data frame should look like the following, with <code>id</code> and <code>n</code> as column names and with 9 rows.</p>
<divclass="cell">
<pre><code>#> # A tibble: 9 × 2
#> id n
#><chr><chr>
#> 1 Brand 1 n
#> 2 1234 8
#> 3 8721 2
#> 4 1822 3
#> 5 Brand 2 n
#> 6 3333 1
#> 7 2156 3
#> 8 3987 6
#> 9 3216 5</code></pre>
</div>
<p>b. Modify <code>sales</code> further to get it into the following tidy format with three columns (<code>brand</code>, <code>id</code>, and <code>n</code>) and 7 rows of data. Note that <code>id</code> and <code>n</code> are numeric, <code>brand</code> is a character variable.</p>
<divclass="cell">
<pre><code>#> # A tibble: 7 × 3
#> brand id n
#><chr><dbl><dbl>
#> 1 Brand 1 1234 8
#> 2 Brand 1 8721 2
#> 3 Brand 1 1822 3
#> 4 Brand 2 3333 1
#> 5 Brand 2 2156 3
#> 6 Brand 2 3987 6
#> 7 Brand 2 3216 5</code></pre>
</div>
</li>
<li><p>Recreate the <code>bake_sale</code> data frame, write it out to an Excel file using the <code><ahref="https://rdrr.io/pkg/openxlsx/man/write.xlsx.html">write.xlsx()</a></code> function from the openxlsx package.</p></li>
<li><p>In <ahref="#chp-data-import"data-type="xref">#chp-data-import</a> you learned about the <code><ahref="https://rdrr.io/pkg/janitor/man/clean_names.html">janitor::clean_names()</a></code> function to turn columns names into snake case. Read the <code>students.xlsx</code> file that we introduced earlier in this section and use this function to “clean” the column names.</p></li>
<li><p>What happens if you try to read in a file with <code>.xlsx</code> extension with <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_xls()</a></code>?</p></li>
<p>This section will also focus on spreadsheets, but this time you’ll be loading data from a Google Sheet with the <strong>googlesheets4</strong> package. This package is non-core tidyverse as well, you need to load it explicitly.</p>
<p>A quick note about the name of the package: googlesheets4 uses v4 of the <ahref="https://developers.google.com/sheets/api/">Sheets API v4</a> to provide an R interface to Google Sheets, hence the name.</p>
<p>The main function of the googlesheets4 package is <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>, which reads a Google Sheet from a URL or a file id. This function also goes by the name <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">range_read()</a></code>.</p>
<p>You can also create a brand new sheet with <code><ahref="https://googlesheets4.tidyverse.org/reference/gs4_create.html">gs4_create()</a></code> or write to an existing sheet with <code><ahref="https://googlesheets4.tidyverse.org/reference/sheet_write.html">sheet_write()</a></code> and friends.</p>
<p>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 <code><ahref="https://readr.tidyverse.org/reference/read_delim.html">read_csv()</a></code> function you’ve seen in <ahref="#chp-data-import"data-type="xref">#chp-data-import</a>. Therefore, many of the tasks can be accomplished with simply swapping out <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> for <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>. 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.</p>
<p><ahref="#fig-students-googlesheets"data-type="xref">#fig-students-googlesheets</a> shows what the spreadsheet we’re going to read into R looks like in Google Sheets. This is the same dataset as in <ahref="#fig-students-excel"data-type="xref">#fig-students-excel</a>, except it’s stored in a Google Sheet instead of Excel.</p>
<divclass="cell">
<divclass="cell-output-display">
<figureid="fig-students-googlesheets"><p><imgsrc="screenshots/import-googlesheets-students.png"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."width="986"/></p>
<figcaption>Google Sheet called students in a browser window.</figcaption>
</figure>
</div>
</div>
<p>The first argument to <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> is the URL of the file to read. You can also access this file via <ahref="https://pos.it/r4ds-students"class="uri">https://pos.it/r4ds-students</a>, however note that at the time of writing this book you can’t read a sheet directly from a short link.</p>
#> 1 1 Sunil Huffmann Strawberry yoghurt Lunch only <dbl>
#> 2 2 Barclay Lynn French fries Lunch only <dbl>
#> 3 3 Jayendra Lyne N/A Breakfast and lunch <dbl>
#> 4 4 Leon Rossini Anchovies Lunch only <NULL>
#> 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch <chr>
#> 6 6 Güvenç Attila Ice cream Lunch only <dbl></pre>
</div>
<p>Just like we did with <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>, we can supply column names, NA strings, and column types to <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>.</p>
#> 1 1 Sunil Huffmann Strawberry yoghurt Lunch only 4
#> 2 2 Barclay Lynn French fries Lunch only 5
#> 3 3 Jayendra Lyne <NA> Breakfast and lunch 7
#> 4 4 Leon Rossini Anchovies Lunch only NA
#> 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch 5
#> 6 6 Güvenç Attila Ice cream Lunch only 6</pre>
</div>
<p>Note that we defined column types a bit differently here, using short codes. For example, “dcccc” stands for “double, character, character, character, character”.</p>
<p>It’s also possible to read individual sheets from Google Sheets as well. Let’s read the penguins Google Sheet at <ahref="https://pos.it/r4ds-penguins"class="uri">https://pos.it/r4ds-penguins</a>, and specifically the “Torgersen Island” sheet in it.</p>
<p>You can obtain a list of all sheets within a Google Sheet with <code><ahref="https://googlesheets4.tidyverse.org/reference/sheet_properties.html">sheet_names()</a></code>:</p>
<p>Finally, just like with <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>, we can read in a portion of a Google Sheet by defining a <code>range</code> in <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>. Note that we’re also using the <code><ahref="https://googlesheets4.tidyverse.org/reference/gs4_examples.html">gs4_example()</a></code> function below to locate an example Google Sheet that comes with the googlesheets4 package.</p>
<p>You can write from R to Google Sheets with <code><ahref="https://googlesheets4.tidyverse.org/reference/sheet_write.html">write_sheet()</a></code>:</p>
<divclass="cell">
<predata-type="programlisting"data-code-language="r">write_sheet(bake_sale, ss = "bake-sale")</pre>
</div>
<p>If you’d like to write your data to a specific (work)sheet inside a Google Sheet, you can specify that with the <code>sheet</code> argument as well.</p>
<divclass="cell">
<predata-type="programlisting"data-code-language="r">write_sheet(bake_sale, ss = "bake-sale", sheet = "Sales")</pre>
</div>
</section>
<sectionid="authentication"data-type="sect2">
<h2>
Authentication</h2>
<p>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 <em>your</em> Google Sheets.</p>
<p>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 <code><ahref="https://googlesheets4.tidyverse.org/reference/gs4_auth.html">gs4_auth()</a></code>, e.g.<code>gs4_auth(email = "mine@example.com")</code>, 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: <ahref="https://googlesheets4.tidyverse.org/articles/auth.html"class="uri">https://googlesheets4.tidyverse.org/articles/auth.html</a>.</p>
<oltype="1"><li><p>Read the <code>students</code> dataset from earlier in the chapter from Excel and also from Google Sheets, with no additional arguments supplied to the <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> and <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> functions. Are the resulting data frames in R exactly the same? If not, how are they different?</p></li>
<li><p>Read the Google Sheet titled survey from <ahref="https://pos.it/r4ds-survey"class="uri">https://pos.it/r4ds-survey</a>, with <code>survey_id</code> as a character variable and <code>n_pets</code> as a numerical variable.</p></li>
<li>
<p>Read the Google Sheet titled roster from <ahref="https://pos.it/r4ds-roster"class="uri">https://pos.it/r4ds-roster</a>. The resulting data frame should be called <code>roster</code> and should look like the following.</p>
<p>In this chapter you learned how to read data into R from spreadsheets: from Microsoft Excel with <code><ahref="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> from the readxl package and from Google Sheets with <code><ahref="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> 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.</p>
<p>On the other hand, writing to an Excel file requires a different package and function (<code><ahref="https://docs.ropensci.org/writexl/reference/write_xlsx.html">writexl::write_xlsx()</a></code>) while you can write to a Google Sheet with the googlesheets4 package, with <code><ahref="https://googlesheets4.tidyverse.org/reference/sheet_write.html">write_sheet()</a></code>.</p>
<p>In the next chapter, you’ll learn about a different data source and how to read data from that source into R: databases.</p>