How to read Google Sheets data into R

Welcome back! Yesterday we talked about the powerful GoogleSheets4 package within R, this allows you to communicate with Google Sheets from your R environment. In this tutorial i’ll break down exactly how to read data from your Google Sheets account into your R environment, let’s get started!

Installation

First off, we want to install the Google Sheets 4 package, to do this, use the following installation line inside of your R console:

install.packages("googlesheets4")

Developing The Code

Awesome, next up we want to import that library into our environment, to do so, use the following line:

#IMPORTING THE LIBRARY
library(googlesheets4)

Next up we want to essentially read the Google Sheet into our enviornment, we have a couple of ways to read it in, we can use the URL of the Google Sheet just like below:

read_sheet("https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/edit#gid=780868077")

You will essentially get a little prompt in our R console which allows you to give access from your Google Account to the API:

A little hard to read, but just follow the prompts and you’ll be good to go. Next up, you want to make your way over to your R console and watch some magic happen, you will eventually see this output in your R console, this will essentially download the data from Google Sheets:

From there, you can store that data within a variable as well:

Awesome! You can also use the Sheet ID from Google Sheets as well:

read_sheet("1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY")

This is the output:

It essentially gives you the same output, it does allow a bit more flexibility.

Congrats! This package has a lot more functionality than this (which i’ll talk about in later articles), but this is a great introduction to dynamically reading your data from a Google Sheet!

As Always

As always, if you have any suggestions, thoughts or just want to connect, feel free to contact / follow me on Twitter! Also, below is a link to some of my favorite resources for learning programming, Python, R, Data Science, etc.

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store