In this exercise set, we will practice using the Google Sheets package to create and manipulate a Google spreadsheet within R.
After completing this exercise set, you will be able to prepare a basic Google Sheets document using just R, leaving behind a reproducible R-script. Note that using Google Sheets is free of cost, but requires you have access to or create a Google account.
Solutions are available here.
Install and load the Google Sheets package, along with its recommended pairing, dplyr. If you haven’t already, take a look at usage examples.
Authenticate your Google account and authorize Google Sheets to access your Google Drive.
Make a new empty sheet/spreadsheet called “iris_r”, containing a single worksheet called “test1.”
Write the built-in Iris data.frame to the “test1” sheet using gs_edit_cells().
Again, write the built-in Iris data.frame to a new worksheet “test2” directly with the function gs_ws_new().
Now, write mtcars’ first 5 rows to a new sheet called “test3”, this time by using gs_add_row(). Note that it seems you first have to add a header and one row to the sheet, so you need to first use gs_edit_cells() to do that.
Create a new sheet called “My favorite R-ex” and add a hyperlink to any exercise set on R-exercises.com with the text “this.”
Now, create a new sheet “test1b.” Get the first four columns names from “test1” by linking to them. Then calculate their averages with Google Sheet formulas.
Use a Google Sheet function to extract the URL of your new document. This way, you can access it quickly in the future (or coworkers, once you have shared access.)
Turns out one of your coworkers doesn’t want to create a Google account! Download the file as an Microsoft Excel document using R and the Google Sheet package.
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below