如何在R内核的Google Colab笔记本中导入私有数据
Great question! It’s totally possible to mount your Google Drive in an R kernel Colab notebook—you just need to work around the Python-focused default tools. Here are two straightforward, reliable methods:
Method 1: Use reticulate to Call Colab’s Python Mount Function
Colab’s R environment comes pre-installed with the reticulate package, which lets you interface directly with Python code. This means you can use the official Google Drive mount function without switching kernels:
# Load the reticulate package (pre-installed in Colab R) library(reticulate) # Import Colab's drive module drive <- import("google.colab.drive") # Trigger the mount process drive$mount("/content/drive")
When you run this code, you’ll see the same authorization prompt as in a Python kernel:
- Click the generated link to sign in to your Google account.
- Copy the authorization code.
- Paste it back into the Colab input box and press Enter.
Once authorized, your Drive will be mounted at /content/drive/MyDrive—you can access files using standard R file functions like read.csv(), list.files(), etc.
Method 2: Use Colab Magic Commands to Run Python Mount Code
Colab’s R kernel supports IPython-style magic commands, so you can directly run the standard Python mount code with the %python magic:
# Run Python's Drive mount code via magic command %python from google.colab import drive drive.mount('/content/drive')
This method is even simpler—you’re reusing the exact same code from Python kernels, just wrapped in the magic command to execute it in the Python runtime alongside your R session. The authorization steps are identical to Method 1.
Verify the Mount
To confirm your Drive is mounted correctly, run this R code to list files in your Drive’s root:
# List files in your Google Drive root list.files("/content/drive/MyDrive")
Either method will give you full access to your private data in the R kernel, so you can read/write files just like you would with local storage.
内容的提问来源于stack exchange,提问作者Parseltongue

