You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中下载共享Google Drive文件时文件损坏的问题求助

解决R中下载共享Google Drive文件损坏的问题

Hey there, I’ve run into this exact issue before! The problem is that the Google Drive links you’re using are web preview URLs (like https://drive.google.com/open?id=XXX), not direct download links. When you use download.file on these, you’re actually downloading the HTML of the Drive preview page instead of the actual file—hence the corrupted files.

Let me walk you through two solid solutions:

方法1:转换为直接下载链接

You can easily convert your existing links to direct download format by swapping out the URL structure. For a link like https://drive.google.com/open?id=FILE_ID, change it to https://drive.google.com/uc?export=download&id=FILE_ID.

Here’s your modified code:

# Load required Packages
requiredPackages <- c("rio", "plm", "splm", "tmaptools", "spdep", "fields", "readxl")
ipak <- function(pkg) {
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}
ipak(requiredPackages)

## Updated to direct download URLs
pdataURL = "https://drive.google.com/uc?export=download&id=1PjDlLiA99-3xuGPhPLltRg8uod6zPAKn" # data1.xlsx
sdataURL = "https://drive.google.com/uc?export=download&id=1VJGL8aSJomvWCnw9FPEWTJsQ65StYdzW" # nuts2a.shp

# Specify file paths and create data folder if needed
pdataDest = file.path("./data/data1.xlsx")
sdataDest = file.path("./data/nut2sa.shp")
if (!dir.exists("./data")) dir.create("./data")

# Download files (note: on Windows, you might need to use method = "curl" if wget isn't installed)
download.file(pdataURL, destfile = pdataDest, method = "wget", mode = "wb")
download.file(sdataURL, destfile = sdataDest, method = "wget", mode = "wb")

# Load and manipulate data as before
pdata <- read_excel(pdataDest)
shape_nuts <- read_shape(sdataDest)

⚠️ Heads up: This method might fail for files larger than 100MB, since Google will prompt a virus scan page that blocks direct downloads. For those cases, use Method 2.

方法2:使用googledrive包(更可靠)

The googledrive package is the official R tool for interacting with Google Drive, and it handles shared files, permissions, and large downloads seamlessly—no URL hacking required.

  1. First, install and load the package:
install.packages("googledrive")
library(googledrive)
  1. You’ll be prompted to log into your Google account and authorize R to access your Drive (since you have view access to the shared folder, this will work perfectly).

  2. Then use drive_download() to grab your files:

# Create data folder if it doesn't exist
if (!dir.exists("./data")) dir.create("./data")

# Download the Excel file using its file ID
drive_download(
  as_id("1PjDlLiA99-3xuGPhPLltRg8uod6zPAKn"),
  path = "./data/data1.xlsx",
  overwrite = TRUE
)

# Download the Shapefile (important: this will grab all associated files like .shx, .dbf automatically)
drive_download(
  as_id("1VJGL8aSJomvWCnw9FPEWTJsQ65StYdzW"),
  path = "./data/nut2sa.shp",
  overwrite = TRUE
)

# Load your data as usual
pdata <- read_excel("./data/data1.xlsx")
shape_nuts <- read_shape("./data/nut2sa.shp")

Quick Extra Tips

  • If you’re on Windows and get errors with method = "wget", try method = "curl" instead (most modern Windows versions have curl pre-installed) or omit the method argument entirely—R will pick the best option for your system.
  • For Shapefiles, make sure all companion files (.shx, .dbf, .prj, etc.) are downloaded. The googledrive package takes care of this automatically, but if you use Method 1, you’ll need to repeat the URL conversion for each file in the Shapefile set.

内容的提问来源于stack exchange,提问作者Technophobe01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:50:16