How to download a shared file from Google Drive in R

How To
A tutorial on how to download a shared file with ‘anyone with the link’ access rights from Google Drive in R.
Author

Rohit Farmer

Published

October 14, 2022

Introduction

To download a shared file with “anyone with the link” access rights from Google Drive in R, we can utilize the googledrive library from the tidyverse package. The method described here will utilize the file ID copied from the shared link. Typically googledrive package is used to work with a Google Drive of an authenticated user. However, since we are downloading a publicly shared file in this tutorial, we will work without user authentication. So, please follow the steps below.

Step 1: Copy the file ID from the share link.

Below is a share link from my Google Drive pointing to an R data frame.

https://drive.google.com/file/d/1vj607etanUVYzVFj_HXkznHTd0Ltv_Y4/view?usp=sharing

This string 1vj607etanUVYzVFj_HXkznHTd0Ltv_Y4 is the file ID that we will use to download.

Step 2: Download the file using an authenticated connection.

if(!require(googledrive)) install.packages("googledrive")
library(googledrive)

drive_deauth()
drive_user()
public_file <-  drive_get(as_id("1vj607etanUVYzVFj_HXkznHTd0Ltv_Y4"))
drive_download(public_file, overwrite = TRUE)
File downloaded:
• hdstim-example-data.rds <id: 1vj607etanUVYzVFj_HXkznHTd0Ltv_Y4>
Saved locally as:
• hdstim-example-data.rds

The downloaded data frame.

library(DT)
datatable(head(readRDS("hdstim-example-data.rds")))
Back to top

Citation

BibTeX citation:
@online{farmer2022,
  author = {Farmer, Rohit},
  title = {How to Download a Shared File from {Google} {Drive} in {R}},
  date = {2022-10-14},
  url = {https://dataalltheway.com/posts/003-how-to-download-a-shared-file-from-googledrive-in-r},
  langid = {en}
}
For attribution, please cite this work as:
Farmer, Rohit. 2022. “How to Download a Shared File from Google Drive in R.” October 14, 2022. https://dataalltheway.com/posts/003-how-to-download-a-shared-file-from-googledrive-in-r.