当前位置: 首页 > news >正文

加载R包unloadNamespace时,我该怎么办

加载R包unloadNamespace时,我该怎么办
当我们的R底层逻辑不清楚的时候,bug就会不经意地来到我们眼前。比如:

library(ggpubr)
Error in value[3L] :
Package ‘ggpubr’ version 0.3.0 cannot be unloaded:
Error in unloadNamespace(package) : namespace ‘ggpubr’ is imported by ‘immunarch’ so cannot be unloaded
In addition: Warning message:
package ‘ggpubr’ was built under R version 3.6.3
这时候,我该怎么办呢?就是把上一层的Namespace,给unload掉,再加载。

unloadNamespace(”immunarch“)
library(ggpubr)
library(immunarch)

在这里插入图片描述

那么,为什么会报这样的错呢?首先我们要知道什么是命名空间:

《维基百科》中说 “命名空间是对作用域的一种特殊的抽象”,在这里我用一个比方来具体说明一下:

比如张三在公司 A,他的工号是 111,李四在公司 B,他的工号也是 111,因为两个人在不同的公司,他们俩的工号可以相同但是不会引起混乱,这里的公司就表示一个独立的命名空间,如果两个人在一个公司的话,他们的工号就不能相同,否则光看工号也不知道到底是谁

我们在载入一个R包的时候也会把他的命名空间导进来,但是如果再导入一样的,就会冲突了。比如这一天店里里来了两个111 ,都带了对象,我们要说:111的对象麻烦结下账。这下麻烦了。

在R里面如何看当前的命名空间呢?

library(Seurat)
loadedNamespaces()
[1] “gridExtra” “gplots” “graphics” “rsvd” “stringi”
[6] “pbapply” “lattice” “bitops” “vctrs” “Rdpack”
[11] “mgcv” “survival” “spatstat.data” “later” “tidyr”
[16] “tensor” “R.utils” “uwot” “data.table” “rstudioapi”
[21] “base” “npsurv” “deldir” “nlme” “ggplot2”
[26] “SDMTools” “htmlwidgets” “future” “listenv” “ggrepel”
[31] “leiden” “tools” “irlba” “parallel” “magrittr”
[36] “grDevices” “methods” “R.oo” “ape” “Rcpp”
[41] “KernSmooth” “promises” “gdata” “RcppParallel” “httr”
[46] “assertthat” “globals” “lifecycle” “R6” “munsell”
[51] “gtools” “png” “R.methodsS3” “digest” “caTools”
[56] “codetools” “sctransform” “splines” “cowplot” “lmtest”
[61] “colorspace” “pkgconfig” “pillar” “lsei” “ROCR”
[66] “xtable” “utils” “abind” “plyr” “reticulate”
[71] “RANN” “gtable” “zoo” “datasets” “stats”
[76] “tidyselect” “purrr” “reshape2” “ica” “fastmap”
[81] “bibtex” “crosstalk” “goftest” “RcppAnnoy” “viridisLite”
[86] “tibble” “rlang” “cluster” “future.apply” “Seurat”
[91] “zeallot” “glue” “Matrix” “spatstat” “scales”
[96] “backports” “metap” “RColorBrewer” “ggridges” “stringr”
[101] “igraph” “Rtsne” “dplyr” “shiny” “polyclip”
[106] “grid” “spatstat.utils” “gbRd” “httpuv” “htmltools”
[111] “yaml” “lazyeval” “plotly” “tsne” “crayon”
[116] “fitdistrplus” “MASS” “jsonlite” “rpart” “mime”
[121] “compiler”
但是这时候,我再加载

library(Rtsne)
是可以的,为什么?

所以,我们还是看一下这个报错的根源:library的源码,他一定是在某个地方加了判断:

library
function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,
logical.return = FALSE, warn.conflicts = TRUE, quietly = FALSE,
verbose = getOption(“verbose”))
{
testRversion <- function(pkgInfo, pkgname, pkgpath) {
if (is.null(built <- pkgInfo B u i l t ) ) s t o p ( g e t t e x t f ( " p a c k a g e s Q u o t e ( p k g n a m e ) ) , c a l l . = F A L S E , d o m a i n = N A ) R v e r s i o n b u i l t u n d e r < − a s . n u m e r i c v e r s i o n ( b u i l t Built)) stop(gettextf("package %s has not been installed properly\n", sQuote(pkgname)), call. = FALSE, domain = NA) R_version_built_under <- as.numeric_version(built Built))stop(gettextf("packagesQuote(pkgname)),call.=FALSE,domain=NA)Rversionbuiltunder<as.numericversion(builtR)
if (R_version_built_under < “3.0.0”)
stop(gettextf(“package %s was built before R 3.0.0: please re-install it”,
sQuote(pkgname)), call. = FALSE, domain = NA)
current <- getRversion()
if (length(Rdeps <- pkgInfoKaTeX parse error: Expected '}', got 'EOF' at end of input: … target <- depversion
res <- if (is.character(target)) {
do.call(dep o p , l i s t ( a s . n u m e r i c ( R . v e r s i o n [ [ " s v n r e v " ] ] ) , a s . n u m e r i c ( s u b ( " r " , " " , d e p op, list(as.numeric(R.version[["svn rev"]]), as.numeric(sub("^r", "", dep op,list(as.numeric(R.version[["svnrev"]]),as.numeric(sub("r","",depversion))))
}
else {
do.call(depKaTeX parse error: Expected 'EOF', got '}' at position 64: … }̲ …op, target),
call. = FALSE, domain = NA)
}
}
if (R_version_built_under > current)
warning(gettextf(“package %s was built under R version %s”,
sQuote(pkgname), as.character(built R ) ) , c a l l . = F A L S E , d o m a i n = N A ) p l a t f o r m < − b u i l t R)), call. = FALSE, domain = NA) platform <- built R)),call.=FALSE,domain=NA)platform<builtPlatform
r_arch <- .Platform r a r c h i f ( . P l a t f o r m r_arch if (.Platform rarchif(.PlatformOS.type == “unix”) {
if (!nzchar(r_arch) && grepl("\w", platform) &&
!testPlatformEquivalence(platform, R.versionKaTeX parse error: Expected 'EOF', got '}' at position 176: … = NA) }̲ else {…Depends))
if (packageHasNamespace(package, which.lib.loc)) {
if (isNamespaceLoaded(package)) {
newversion <- as.numeric_version(pkgInfoKaTeX parse error: Undefined control sequence: \n at position 601: …onMessage(e), "\̲n̲")), domain = N…DESCRIPTION
if (“Encoding” %in% names(txt)) {
to <- if (Sys.getlocale(“LC_CTYPE”) == “C”)
“ASCII//TRANSLIT”
else “”
tmp <- try(iconv(txt, from = txt[“Encoding”],
to = to))
if (!inherits(tmp, “try-error”))
txt <- tmp
else warning("‘DESCRIPTION’ has an ‘Encoding’ field and re-encoding is not possible",
call. = FALSE)
}
nm <- paste0(names(txt), “:”)
formatDL(nm, txt, indent = max(nchar(nm, “w”)) +
3L)
}
else if (basename(f) %in% “vignette.rds”) {
txt <- readRDS(f)
if (is.data.frame(txt) && nrow(txt))
cbind(basename(gsub("\.[[:alpha:]]+ " , " " , t x t ", "", txt ","",txtFile)), paste(txt T i t l e , p a s t e 0 ( r e p . i n t ( " ( s o u r c e " , N R O W ( t x t ) ) , i f e l s e ( n z c h a r ( t x t Title, paste0(rep.int("(source", NROW(txt)), ifelse(nzchar(txt Title,paste0(rep.int("(source",NROW(txt)),ifelse(nzchar(txtPDF), “, pdf”,
“”), “)”)))
else NULL
}
else readLines(f)
}
for (i in which(file.exists(docFiles))) pkgInfo[[i]] <- readDocFile(docFiles[i])
y <- list(name = pkgName, path = pkgPath, info = pkgInfo)
class(y) <- “packageInfo”
return(y)
}
else {
if (is.null(lib.loc))
lib.loc <- .libPaths()
db <- matrix(character(), nrow = 0L, ncol = 3L)
nopkgs <- character()
for (lib in lib.loc) {
a <- .packages(all.available = TRUE, lib.loc = lib)
for (i in sort(a)) {
file <- system.file(“Meta”, “package.rds”, package = i,
lib.loc = lib)
title <- if (nzchar(file)) {
txt <- readRDS(file)
if (is.list(txt))
txt <- txt$DESCRIPTION
if (“Encoding” %in% names(txt)) {
to <- if (Sys.getlocale(“LC_CTYPE”) == “C”)
“ASCII//TRANSLIT”
else “”
tmp <- try(iconv(txt, txt[“Encoding”], to,
“?”))
if (!inherits(tmp, “try-error”))
txt <- tmp
else warning("‘DESCRIPTION’ has an ‘Encoding’ field and re-encoding is not possible",
call. = FALSE)
}
txt[“Title”]
}
else NA
if (is.na(title))
title <- " ** No title available ** "
db <- rbind(db, cbind(i, lib, title))
}
if (length(a) == 0L)
nopkgs <- c(nopkgs, lib)
}
dimnames(db) <- list(NULL, c(“Package”, “LibPath”, “Title”))
if (length(nopkgs) && !missing(lib.loc)) {
pkglist <- paste(sQuote(nopkgs), collapse = ", ")
msg <- sprintf(ngettext(length(nopkgs), “library %s contains no packages”,
“libraries %s contain no packages”), pkglist)
warning(msg, domain = NA)
}
y <- list(header = NULL, results = db, footer = NULL)
class(y) <- “libraryIQR”
return(y)
}
if (logical.return)
TRUE
else invisible(.packages())
}
<bytecode: 0x08267d50>
<environment: namespace:base>
果不其然:

        if (packageHasNamespace(package, which.lib.loc)) {
            if (isNamespaceLoaded(package)) {
              newversion <- as.numeric_version(pkgInfo$DESCRIPTION["Version"])
              oldversion <- as.numeric_version(getNamespaceVersion(package))
              if (newversion != oldversion) {
                tryCatch(unloadNamespace(package), error = function(e) {
                  P <- if (!is.null(cc <- conditionCall(e))) 
                    paste("Error in", deparse(cc)[1L], ": ")
                  else "Error : "
                  stop(gettextf("Package %s version %s cannot be unloaded:\n %s", 
                    sQuote(package), oldversion, paste0(P, 
                      conditionMessage(e), "\n")), domain = NA)
                })
              }

遇到这种情况往往是从两个不同的地方导入包导致的。

https://github.com/RevolutionAnalytics/checkpoint/issues/155
https://stackoverflow.com/questions/37031457/problems-trying-to-install-package-in-r
https://zhuanlan.zhihu.com/p/42399724

作者:周运来就是我
链接:https://www.jianshu.com/p/c0f3
Error in unloadNamespace(c(“dplyr”, “recipes”)) :
名字空间‘dplyr’是由‘recipes’, ‘plotly’, ‘dbplyr’, ‘tidyr’, ‘broom’引进的,因此无法将之卸下
In addition: Warning message:
In ns == loadedNamespaces() :
longer object length is not a multiple of shorter object length

library(nichenetr)
Error: package or namespace load failed for ‘nichenetr’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
载入了名字空间‘glue’ 1.4.2,但需要的是>= 1.5.0
library(glue)
Error in value[3L] :
Package ‘glue’ version 1.4.2 cannot be unloaded:
Error in unloadNamespace(package) : 名字空间‘glue’是由‘dplyr’, ‘dbplyr’, ‘tidyr’, ‘ggplot2’, ‘usethis’, ‘broom’, ‘tidyselect’引进的,因此无法将之卸下
In addition: Warning message:
程辑包‘glue’是用R版本4.1.3 来建造的
unloadNamespace(“glue”)
Error in unloadNamespace(“glue”) :
名字空间‘glue’是由‘dplyr’, ‘dbplyr’, ‘tidyr’, ‘ggplot2’, ‘usethis’, ‘broom’, ‘tidyselect’引进的,因此无法将之卸下

相关文章:

  • 4.7 type( ) 函数查询数据类型
  • 4.8 数据类型转换
  • 用R提取PDF文本并创建整洁的数据
  • 4.9 Python 列表
  • 4.10 Python 元组
  • clustree确定多少个cluster
  • 4.11 python 字典
  • 4.12 input( ) 输入函数 及注释
  • 4.13 Python 索引与切片
  • 4.14 python 字符串的拼接及格式化(.format)
  • ggplot画 ump 和tsne 从seurat中使用addmodule得到的umap 使用ggplot画图
  • 5.1 Python 算术运算符
  • ggplot画梯度颜色图 不同颜色 对seurat的细胞类型进行inflammatory 炎症打分 addmodule
  • cell annotation细胞注释 给已经做成seurat对象的细胞统一改换名字 换标签 加标签
  • 5.2 Python 赋值运算符
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 【译】理解JavaScript:new 关键字
  • Gradle 5.0 正式版发布
  • LeetCode29.两数相除 JavaScript
  • Python代码面试必读 - Data Structures and Algorithms in Python
  • rc-form之最单纯情况
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • spring boot下thymeleaf全局静态变量配置
  • SQLServer之创建显式事务
  • v-if和v-for连用出现的问题
  • Vue 2.3、2.4 知识点小结
  • Vue2 SSR 的优化之旅
  • vue中实现单选
  • 聊聊flink的BlobWriter
  • 前端代码风格自动化系列(二)之Commitlint
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 如何使用 JavaScript 解析 URL
  • 我与Jetbrains的这些年
  • 学习笔记TF060:图像语音结合,看图说话
  • 移动端唤起键盘时取消position:fixed定位
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • mysql面试题分组并合并列
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • #android不同版本废弃api,新api。
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (2)nginx 安装、启停
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (4)logging(日志模块)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (理论篇)httpmoudle和httphandler一览
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一)插入排序
  • (原)Matlab的svmtrain和svmclassify
  • (转)jQuery 基础
  • (转)socket Aio demo