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

在R中安装ggmap出现依赖错误:png包加载失败

安装ggmap及相关依赖时的libz.1.dylib缺失问题

尝试的安装代码

install.packages("png")
install.packages("RgoogleMaps")
install.packages("ggmap")
library(ggmap)

报错信息

installing to /usr/local/Cellar/r/4.2.2/lib/R/library/00LOCK-png/00new/png/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘png’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/usr/local/Cellar/r/4.2.2/lib/R/library/00LOCK-png/00new/png/libs/png.so':
  dlopen(/usr/local/Cellar/r/4.2.2/lib/R/library/00LOCK-png/00new/png/libs/png.so, 6): Library not loaded: @rpath/libz.1.dylib
  Referenced from: /usr/local/Cellar/r/4.2.2/lib/R/library/00LOCK-png/00new/png/libs/png.so
  Reason: image not found
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/usr/local/Cellar/r/4.2.2/lib/R/library/png’
Warning in install.packages :
  installation of package ‘png’ had non-zero exit status
ERROR: dependency ‘png’ is not available for package ‘RgoogleMaps’
* removing ‘/usr/local/Cellar/r/4.2.2/lib/R/library/RgoogleMaps’
Warning in install.packages :
  installation of package ‘RgoogleMaps’ had non-zero exit status
ERROR: dependencies ‘RgoogleMaps’, ‘png’ are not available for package ‘ggmap’
* removing ‘/usr/local/Cellar/r/4.2.2/lib/R/library/ggmap’
Warning in install.packages :
  installation of package ‘ggmap’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/fd/7vyffy191_l3j6fhyqbvsn0r0000gn/T/RtmptKSXHF/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Error in library(ggmap) : there is no package called ‘ggmap’

解决方案

问题根源是系统缺少zlib库,或R无法正确定位该库路径。按以下步骤操作:

  1. 通过Homebrew安装zlib(若未安装):

    brew install zlib
    
  2. 在R中设置环境变量,让png包安装时能找到zlib:

    Sys.setenv(ZLIB_LIBS="-L/usr/local/opt/zlib/lib")
    Sys.setenv(ZLIB_CFLAGS="-I/usr/local/opt/zlib/include")
    install.packages("png", type = "source")
    
  3. 成功安装png包后,继续安装剩余依赖和ggmap:

    install.packages("RgoogleMaps")
    install.packages("ggmap")
    library(ggmap)
    

若上述步骤无效,建议重新通过Homebrew安装R,确保系统依赖正确链接:

brew reinstall r

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 12:55:11