Hey folks! I’m trying to grab a CSV file from my Google Drive using R, but I’m hitting a wall. Here’s what I’ve done so far:
library(googledrive)
# Set up Google Drive access
drive_auth()
# Look for the file
file_search <- drive_find(pattern = "yearly_data_mainland")
# Try to download
drive_download(file_search, verbose = TRUE)
But I’m getting this error:
Error in curl::curl_fetch_disk(url, file_path, handle = handle) :
Failed to open file C:\data\yearly_data_mainland_2023-09-15.csv.
I’m stumped! Any ideas why this isn’t working?
Update: I tried a test. I downloaded the CSV, removed the last column (called ‘location’), renamed it to ‘test.csv’, and uploaded it again. Surprisingly, I could download that one! Could there be an issue with my original CSV column names or filename? Any help would be awesome!
Have you considered using the googledrive package in combination with readr? This approach might solve your issue. First, authenticate with drive_auth(), then use drive_get() to locate your file. Instead of downloading, you can stream the file content directly into R using drive_read_string(). Finally, use read_csv() from the readr package to parse the CSV data.
This method bypasses the need to save the file locally, which could be causing your current error. It is also more efficient for handling large files. Let me know if this resolves your problem.
hey sophiac, sounds like a permissions issue. try checking if ur google account has access to that specific file. also, make sure ur working directory in R is set correctly. u could try using setwd() before running the download command. hope this helps!
I’ve encountered similar issues before. One thing that often gets overlooked is file size. If your CSV is particularly large, it might be causing timeout problems during the download process. You could try increasing the timeout limit using the httr::config() function.
Another potential issue could be special characters in your column names. R can be finicky with certain characters, especially in file operations. Try simplifying your column names to just alphanumeric characters and underscores.
Lastly, consider using the googlesheets4 package instead. It’s designed specifically for Google Sheets and might handle the download more smoothly. You’d use read_sheet() to directly import the data into R without downloading the file first.
Remember to double-check your file sharing settings in Google Drive too. Sometimes that’s the culprit, even when you think everything’s set up correctly.