How to upload data from Google Drive to Google Colab in an R environment?
How to use Google Colab for Python (power tool to analyze data)?
In my previous post, I introduced how to upload data from Google Drive to Google Colab in a Python environment. Google Colab is primarily Python-based, but now we can change the runtime to R and use R code in Google Colab.
Today, I will introduce how to upload data from Google Drive to Google Colab in an R environment.
First, let’s upload a data file to Google Drive and check its file path. To easily check the file path, you can set the runtime type to Python 3

and run the following code.
from google.colab import drive
drive.mount('/content/drive')
Then, by right-clicking on the file you uploaded, you can find the file path and copy and paste it.
This drive mount should be completed before changing the runtime to R, because this process sets the file path for use in the R environment.

Next, you need to change the runtime type to R, as we are going to work in an R environment.

Now, let’s upload data from your Google Drive to Google Colab.
0) Mount drive to Goolge Colab in Python environment
from google.colab import drive
drive.mount('/content/drive')
In R environment
1) install Python packages
system("pip install -q --upgrade gdown openpyxl")
2) set up drive mount function
drive_mount= function() {
system("pip install -q --upgrade gdown")
system("from google.colab import drive; drive.mount('/content/drive')", intern= TRUE)
}
drive_mount()
3) install the package to upload Excel file
if(!require(readxl)) install.packages("readxl")
library(readxl)
4) Set up file pathway
file_path="/content/drive/MyDrive/project/mydata.xlsx"
df=read_excel(file_path)
# Please check your file pathway
print(df)
Then you can upload your data to Google Colab in R environment.
