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

升级R至4.2.1后使用openxlsx读取URL中xlsx文件超时问题求助

问题:R 4.2.1中read.xlsx读取URL文件超时,回退R 4.0.5恢复正常

问题详情

  • 升级Rgui至4.2.1版本后,先出现R文件打开为空的问题,将默认编码改为UTF-8后解决。
  • 随后触发新问题:使用read.xlsx直接读取URL中的xlsx文件时,出现超时错误,错误信息如下:

In download.file(url = xlsxFile, destfile = tmpFile, cacheOK = FALSE, :
URL 'https://www.rug.nl/ggdc/historicaldevelopment/maddison/data/mpd2020.xlsx': Timeout of 100000 seconds was reached

  • 回退至R 4.0.5版本后,该问题消失。
  • 可复现代码:
url_maddison = "https://www.rug.nl/ggdc/historicaldevelopment/maddison/data/mpd2020.xlsx"
read.xlsx(url_maddison, sheet = 3) 

可行解决方案

1. 手动下载后读取本地文件

绕开read.xlsx内置的下载逻辑,先手动下载文件到本地再读取,避免版本兼容问题:

# 以二进制模式下载文件,防止文件损坏
download.file(url = "https://www.rug.nl/ggdc/historicaldevelopment/maddison/data/mpd2020.xlsx",
              destfile = "mpd2020.xlsx",
              mode = "wb")

# 读取本地文件
read.xlsx("mpd2020.xlsx", sheet = 3)

2. 指定下载方法参数

read.xlsx内部调用download.file,可以通过传递download.file.method参数切换下载方式:

  • Windows系统尝试:
read.xlsx(url_maddison, sheet = 3, download.file.method = "wininet")
  • Linux/macOS系统尝试:
read.xlsx(url_maddison, sheet = 3, download.file.method = "curl")

3. 检查并调整网络代理设置

R 4.2.x版本对网络代理的处理逻辑可能有变化,先检查当前代理配置:

Sys.getenv("http_proxy")
Sys.getenv("https_proxy")

若存在代理,临时取消后测试:

Sys.unsetenv("http_proxy")
Sys.unsetenv("https_proxy")
read.xlsx(url_maddison, sheet = 3)

4. 更新相关包

确认read.xlsx所属包(通常为openxlsx或readxl),更新到最新版本:

# 若使用openxlsx包
install.packages("openxlsx")
# 若使用readxl包
install.packages("readxl")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 21:31:15