Analytics Machine Learning R

How to Connect R to Google Sheet and Use

Connect R to Google sheet is one of the common requirements I have come across in recent days. There are times when I get the data in the Google sheet and need to make a certain transformation using R and save it back.

Or, there are times when I get data in the CSV file and after some data transformation, need to save it back to Google sheet for the further uses. In such cases, R has been quite helpful!

Connect R to Google SheetIf you’ve a similar requirement where you need the interaction of R with Google sheet, this article is for you. Here we’ll see how to connect R to Google sheet and work on it.

In this article, I am going to touch the following-

• R Package for connecting R to Google Sheet
• Accessing Google sheet files in R
• Making changes to the existing sheets of Google sheet using R
• Saving an R dataset to Google sheet

All the above-mentioned operations are basic for R connection to Google sheet.

Steps to connect R to Google Sheet and use it

Just follow the below steps to connect R to Google sheet and get started with the data analytics.

Step 1: Install the “googlesheets” packages in R

Open your R interface and install the packages googlesheets. Once installed, initialize the packages to use. You can use the below code to install and initialize the package-


Step 2: Authenticate your Google account

Once you have installed and loaded the googlesheets package, you need to authenticate your Google account from where you need to access and use Google sheet. To authenticate your Google account, you can use the below code-

 gs_auth(new_user = TRUE)

This will take you to the browser and ask you to choose the Google account from where you’ll use Google sheet.

authenticate r to Google account

R code will take you to select Goggle Account

Once you’ve selected your Google account, it will ask you to allow the access. This is required because R’s package tidyverse need to manage your Google sheet.

verify Google account for R to Use

Allow for R to manage the Google sheets

Just click on the allow button and you’ll receive a page showing the successful message. Now you’re safe to close the page and get back to R with your work.

Step 3: Check your Google sheets in R

If your Google account has already some Google sheets which you can see in the R. For this you can simply use the below code-

Check Google sheets in R

Shows all the available sheets in Google account connected

This says, I have 2 files on Google sheet and those are displayed above with the details.

Google sheet files

Files Available in Google sheet account

Step 4: Read the Google sheet data in R

I have a Google sheet with the name “mtcars” on my Google account. I am going to read that Google sheet in R. This has the same mtcars dataset.

 for_gs <- gs_title("for googlesheets")
read Google sheet file using R

Read a sheet from Google account using R code

We can read the above-loaded file using the function gs_read()

 for_gs_sheet <- gs_read(for_gs)
Read Google sheet in r

Read the Google sheet file using R

Let’s check the structure of the loaded file “for_gs_sheet”

structure of Google sheet file

Structure of the read Google sheet in R


Step 5: Let’s modify the “mtcars” Google sheet

We can make changes to the read Google sheet “mtcars” using the function gs_edit_cells().

Please follow the below section of my Google sheet-
Google sheet file details
Now I am going to change the value of A2 and B2 cell to 1 and 2 using the below code-

 gs_edit_cells(for_gs, ws = "mtcars_dataset", anchor = "A2", input = c(4,5), byrow = TRUE)


  • gs_edit_cells: This is the function used to edit the Google sheet
  • for_gs: This is the variable where we have stored the read file
  • ws: It refers to the sheet name in the Google sheet. My sheet name is “mtcars_dataset”
  • anchor: refers to the cell from which the modification will begin. Here I am starting from A2 cell
  • input: the values which we want to update. As I have started from A2 and provided 2 values and so, this will update A2 and B2.
  • Byrow: indicates that the modification will apply horizontally. If this is vertical, A2 and A3 cells will change
make Google sheet file using R

Update Google sheet data using R

If we’ll make byrow= False, A2 and A3 cells will be updated as shown below-

make changes in Google sheet using R code

Make changes in Google sheet using R Code vertically

Step 6: Create a Google Sheet using R and save data to Google sheet

We can create a Google sheet using R using function gs_new(). Let’ create a new Google sheet with the name “Demo Sheet”.

 gs_new(title = "Demo Sheet")
create new Google sheet using R

Create a new Google sheet using R

This has created a blank google sheet with the name “Demo Sheet”

created new google sheet using R code

Create Google sheet using R Code

Now let’s save some data to the worksheet and save it to Google drive-

 gs_new(title = "IRIS Dataset", ws_title = "first_sheet", input = iris)

Here I am creating a new sheet with the name “IRIS Dataset” with the data from IRIS dataset.
Details of the created Google sheet file using R
Please note, we can add several Google sheets with the same name. In that case, those sheets will be differentiated by the “key”.

Conclusion- connect R to Google sheet

This was the basic guide on how to connect R to Google sheet and do the basic operation on Google sheet using R.

If your work also demands, managing the data from the Google sheet, this guide will be helpful to you. Please try and reach to me for any query. You can check more about the package googlesheets here.

You may also check this video guide on the same-

Leave a Comment