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

Ubuntu环境下R脚本调用旧版ggplot2 3.3.6遇加载报错求助

解决方案

1. 重启RStudio后ggplot2自动加载的原因与解决

这是RStudio默认的会话恢复特性,会自动恢复上次关闭时的工作环境,包括已加载的包。解决方式:

  • 打开RStudio设置:Tools → Global Options → General
  • 取消勾选「Restore .RData into workspace at startup」
  • 将「Save workspace to .RData on exit」改为「Never」
  • 另外检查~/.Rprofile或项目目录下的.Rprofile文件,若存在自动加载ggplot2的代码(比如library(ggplot2)),注释掉即可。

2. 含ggimage脚本中切换旧版ggplot2的办法

因为ggfun、ggplotify、ggimage这些包会依赖并锁定ggplot2的命名空间,一旦新版加载就无法卸载替换,所以要调整加载顺序:

  • 优先加载旧版ggplot2:在脚本最开头先执行加载旧版的命令,再调用ggimage相关函数:
# 脚本第一行就加载旧版ggplot2
library(ggplot2, lib.loc="~/R/ggplot336/")
# 之后再使用ggimage
ggimage::geom_image(...)
  • 使用全新会话运行脚本:避免RStudio的会话干扰,可在终端用Rscript your_script.R执行,或在RStudio中选择「Run in Terminal」运行脚本,确保会话初始无加载的ggplot2,再加载旧版。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:50:31