You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

加载ggplot2与survminer包时出现UseMethod相关错误求助

解决R加载ggplot2/survminer时的conditionCall方法错误

首先,先还原你遇到的报错场景:

> install.packages(c("survival", "survminer", "ggplot2"))
Installing packages into ‘C:/Users/xu081/Documents/R/win-library/3.4’ (as ‘lib’ is unspecified)
trying URL 'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.4/survival_2.42-3.zip'
Content type 'application/zip' length 4584781 bytes (4.4 MB)
downloaded 4.4 MB
trying URL 'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.4/survminer_0.4.2.zip'
Content type 'application/zip' length 2849057 bytes (2.7 MB)
downloaded 2.7 MB
trying URL 'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.4/ggplot2_2.2.1.zip'
Content type 'application/zip' length 2784778 bytes (2.7 MB)
downloaded 2.7 MB
package ‘survival’ successfully unpacked and MD5 sums checked
Warning in install.packages : cannot remove prior installation of package ‘survival’
package ‘survminer’ successfully unpacked and MD5 sums checked
package ‘ggplot2’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\xu081\AppData\Local\Temp\RtmpuOtZaF\downloaded_packages
> library(survival)
> library(ggplot2)
Error in UseMethod("conditionCall") : 
  no applicable method for 'conditionCall' applied to an object of class "character"
> library(survminer)
载入需要的程辑包:ggplot2
Error in UseMethod("conditionCall") : 
  no applicable method for 'conditionCall' applied to an object of class "character"
> install.packages("devtools")
Installing package into ‘C:/Users/xu081/Documents/R/win-library/3.4’ (as ‘lib’ is unspecified)
trying URL'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.4/devtools_1.13.5.zip'
Content type 'application/zip' length 443954 bytes (433 KB)
downloaded 433 KB
package ‘devtools’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\xu081\AppData\Local\Temp\Rtmpa62LXv\downloaded_packages
> library(devtools)
> devtools::install_github("tidyverse/ggplot2")
Downloading GitHub repo tidyverse/ggplot2@master from URL https://api.github.com/repos/tidyverse/ggplot2/zipball/master
Installing ggplot2
"C:/PROGRA~1/R/R-34~1.4/bin/x64/R" --no-site-file --no-environ --no-save 
no-restore --quiet CMD INSTALL \
  "C:/Users/xu081/AppData/Local/Temp/Rtmpa62LXv/devtools1dac50bf8ef/tidyverse-ggplot2-4463da6" \
  --library="C:/Users/xu081/Documents/R/win-library/3.4" --install-tests
* installing *source* package 'ggplot2' ...
** R
** data
*** moving datasets to lazyload DB
** inst
** tests
** preparing package for lazy loading
Error in UseMethod("conditionCall") : 
  "conditionCall"没有适用于"character"目标对象的方法
* removing 'C:/Users/xu081/Documents/R/win-library/3.4/ggplot2'
* restoring previous 'C:/Users/xu081/Documents/R/win-library/3.4/ggplot2'
In R CMD INSTALL
Error in UseMethod("conditionMessage") : 
  no applicable method for 'conditionMessage' applied to an object of class "character"

问题原因分析

从报错信息来看,核心问题出在R版本过旧(你用的是R 3.4.x,这是2018年的版本),同时伴随旧包残留的权限/冲突问题:

  1. 新版的ggplot2survminer已经不再支持R 3.4.x,你尝试从GitHub安装最新版ggplot2必然失败;
  2. 安装survival时提示无法移除旧版本,说明包文件被占用或者你没有写入权限,导致新旧包文件冲突,触发了底层函数conditionCall的兼容性错误。

解决方案

方案1:升级R版本(最推荐,一劳永逸)

R 3.4.x已经被主流包淘汰,升级到最新稳定版是根本解决方法:

  1. 关闭所有R会话,卸载当前的R 3.4.x;
  2. 下载最新的Windows版R并安装(注意选择适合你系统的版本);
  3. 打开新安装的R,执行以下命令重新安装包:
install.packages(c("survival", "survminer", "ggplot2"), repos = "https://mirrors.tongji.edu.cn/CRAN/")
library(survival)
library(ggplot2)
library(survminer)

方案2:保留R 3.4.x,修复包冲突

如果暂时无法升级R,按以下步骤清理并重新安装兼容版本的包:

  1. 彻底清理旧包
    • 关闭所有R会话,右键点击R图标选择「以管理员身份运行」;
    • 执行卸载命令:
      remove.packages(c("survival", "ggplot2", "survminer"))
      
    • 手动打开包路径C:/Users/xu081/Documents/R/win-library/3.4,删除survivalggplot2survminer的文件夹(如果还存在);
  2. 重新安装兼容版本的包
    # 安装指定版本的包,确保兼容R 3.4.x
    install.packages(
      c("survival", "ggplot2", "survminer"),
      version = c("2.42-3", "2.2.1", "0.4.2"),
      repos = "https://mirrors.tongji.edu.cn/CRAN/",
      dependencies = TRUE
    )
    
    如果上述命令报错,用devtools安装指定版本:
    library(devtools)
    install_version("ggplot2", version = "2.2.1", repos = "https://mirrors.tongji.edu.cn/CRAN/")
    install_version("survival", version = "2.42-3", repos = "https://mirrors.tongji.edu.cn/CRAN/")
    install_version("survminer", version = "0.4.2", repos = "https://mirrors.tongji.edu.cn/CRAN/")
    

方案3:检查权限问题

如果安装时一直提示无法移除旧包,大概率是权限问题:

  • 确保没有其他R会话或软件在占用包文件;
  • 始终以管理员身份运行R来安装/卸载包;
  • 检查包文件夹C:/Users/xu081/Documents/R/win-library/3.4的写入权限,确保你的用户账号有修改权限。

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

火山引擎 最新活动