Explain how to bring my Googlesheets to R studio like I am 5 years old

Eunjeong Hyeon
2 min readMar 12, 2022

--

Let’s suppose that you are doing your first data analysis project. You already have cleaned the data in Google Sheets and you know that it would be easier to see patterns if you can visualize the data in R studio. And yet, you don’t know how to bring the cleaned data to R studio. Let me explain it to you in the easiest way.

First, install the Googlesheets4 packages and bring the package.

install.packages('googlesheets4') 
library(googlesheets4)

Second, authorize Googlesheets4 to view and manage your Google Sheets.

gs4_auth(
email = gargle::gargle_oauth_email(),
path = NULL,
scopes = "https://www.googleapis.com/auth/spreadsheets",
cache = gargle::gargle_oauth_cache(),
use_oob = TRUE,
token = NULL
)

You might already have tried the code below but the thing is you have to twist the ‘use_oob’ part a little bit because you are working within a browser.

gs4_auth(
email = gargle::gargle_oauth_email(),
path = NULL,
scopes = "https://www.googleapis.com/auth/spreadsheets",
cache = gargle::gargle_oauth_cache(),
use_oob = gargle::gargle_oob_default(),
token = NULL
)

While the code is operating, you can see the question below on your console.

Is it OK to cache OAuth access credentials in the folder
~/.cache/gargle between R sessions?

1: Yes
2: No

Selection:1

Type 1 after ‘Selection: ’

Afterward, you can see the new page comes out. In the page, you can find ‘authorization code’. Copy and paste the code after ‘Enter authorization code:’

Finally, you have succeeded in connecting the Google Sheets to the R studio. Let’s confirm that it worked out well.

data1 <- read_sheet('url address of your google sheets')

By using ‘read_sheet’ function, you have brought the data you want and saved it in data1. If you want to see a few data on the front, you can see it by using function ‘head’.

head(data1)

Thanks for reading my article!

I referred to Google Sheets4 document while writing this but the easy explanation is mine.

--

--