R语言无法解压文件:航运与夜光数据处理报错求助
问题描述
作为R语言新手(使用Rbase 4.3),在处理全球航运密度数据和夜光数据时遇到以下问题:
核心错误:unzip未找到
下载航运密度数据后,调用外部解压脚本时触发错误:
> source("https://raw.githubusercontent.com/milos-agathon/shipping-traffic-maps/main/R/decompress_file.r") > decompress_file( + directory = getwd(), + file = destfile + ) Error in system2("unzip", args = c("-o", file), stdout = TRUE) : '"unzip"' not found
连锁错误
因无法解压得到shipdensity_global.tif文件,后续代码触发一系列对象未找到错误,例如:
> global_traffic <- terra::rast(rastfile) Error: [rast] file does not exist: shipdensity_global.tif
> shipping_traffic <- terra::crop( + x = global_traffic, + y = bounding_box, + snap = "in" + ) Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'crop': object 'global_traffic' not found
同时,下载数据时出现wininet方法已弃用的警告。
解决方案
1. 解决unzip未找到问题
unzip是系统级工具,R依赖它执行解压操作,需根据操作系统安装或配置:
Windows
- 快速替代方案:跳过外部脚本,直接使用R内置的
unzip()函数:unzip("shipdensity_global.zip", exdir = getwd()) - 永久解决:安装Git for Windows(勾选"Add Git to PATH"),或安装GNU Unzip并将其路径添加到系统
PATH环境变量。
macOS
打开终端执行以下命令安装unzip:
# 先安装Homebrew(若未安装) /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # 安装unzip brew install unzip
Linux
根据发行版执行命令:
- Debian/Ubuntu:
sudo apt-get update && sudo apt-get install unzip - CentOS/RHEL:
sudo yum install unzip - Fedora:
sudo dnf install unzip
2. 修复连锁错误
解决unzip问题后,重新执行以下步骤:
- 重新解压航运数据(若之前解压失败):
unzip("shipdensity_global.zip", exdir = getwd()) - 加载tif文件:
global_traffic <- terra::rast("shipdensity_global.tif") - 继续执行裁剪、清洗等后续代码,此时
global_traffic、shipping_traffic等对象将正常创建,夜光数据部分的resample函数也能找到依赖的shipping_traffic_clean对象。
3. 消除wininet弃用警告
修改download.file调用,使用libcurl方法替代已弃用的wininet:
download.file( url = url, destfile = destfile, mode = "wb", method = "libcurl" )
内容的提问来源于stack exchange,提问作者iqbalhabibie habibie
相关产品推荐
相关产品推荐

