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

Mac系统下使用R从GitHub下载.tsv.gz压缩文件失败求助

在Mac上用R从GitHub下载.tsv.gz文件的解决方案

你遇到的问题核心是GitHub的blob链接指向的是文件展示网页,而非直接的文件资源,直接用download.file()会下载HTML页面而非目标压缩文件。以下是可行的解决步骤:

1. 使用GitHub Raw文件链接

将原链接中的blob替换为raw,得到直接的文件下载地址:
原链接:https://github.com/hadley/mastering-shiny/blob/main/neiss/injuries.tsv.gz
替换后:https://github.com/hadley/mastering-shiny/raw/main/neiss/injuries.tsv.gz

2. 下载并读取文件

使用download.file()时,指定method="curl"(适配Mac系统)和mode="wb"(二进制模式下载压缩文件),确保下载的文件完整可用:

# 设置raw文件链接和保存路径
file_url <- "https://github.com/hadley/mastering-shiny/raw/main/neiss/injuries.tsv.gz"
dest_path <- "injuries.tsv.gz"

# 下载文件
download.file(file_url, destfile = dest_path, method = "curl", mode = "wb")

# 解压并读取.tsv文件(使用readr包更高效)
library(readr)
injuries_data <- read_tsv(gzfile(dest_path))

# 若使用base R:
# gunzip(dest_path)
# injuries_data <- read.table("injuries.tsv", sep = "\t", header = TRUE, stringsAsFactors = FALSE)

补充说明

  • 若遇到SSL认证问题,可在download.file()中添加extra = "--insecure"参数(仅在信任资源时使用):
    download.file(file_url, destfile = dest_path, method = "curl", mode = "wb", extra = "--insecure")
    
  • 确保你的R环境已安装readr包(若使用该方案),可通过install.packages("readr")安装。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 21:42:37