如何在不重新启动 R 的情况下卸载包 [英] How to unload a package without restarting R

查看:20
本文介绍了如何在不重新启动 R 的情况下卸载包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想卸载一个包而不必重新启动 R(主要是因为在我尝试不同的、冲突的包时重新启动 R 会令人沮丧,但可以想象,这可以在程序中使用一个函数,然后使用另一个 -- 尽管命名空间引用可能是一个更好的主意).

?library 没有显示任何可以卸载包的选项.

有一个 建议 detach 可以卸载包,但是下面两个都失败了:

分离(素食主义者)

<块引用>

detach(vegan) 错误:name 参数无效

detach("vegan")

<块引用>

detach("vegan") 错误:name 参数无效

那么我该如何卸载一个包呢?

解决方案

试试这个(查看?detach了解更多详情):

detach("package:vegan", unload=TRUE)

<小时>

可以同时加载一个包的多个版本(例如,如果您在不同的库中有一个开发版本和一个稳定版本).为保证所有副本均已分离,请使用此功能.

detach_package <- function(pkg, character.only = FALSE){if(!character.only){pkg <- deparse(替代(pkg))}search_item <- paste("包", pkg, sep = ":")同时(搜索项目 %in% 搜索()){分离(search_item,卸载 = TRUE,character.only = TRUE)}}

用法是,例如

detach_package(素食主义者)

detach_package("vegan", TRUE)

I'd like to unload a package without having to restart R (mostly because restarting R as I try out different, conflicting packages is getting frustrating, but conceivably this could be used in a program to use one function and then another--although namespace referencing is probably a better idea for that use).

?library doesn't show any options that would unload a package.

There is a suggestion that detach can unload package, but the following both fail:

detach(vegan)

Error in detach(vegan) : invalid name argument

detach("vegan")

Error in detach("vegan") : invalid name argument

So how do I unload a package?

解决方案

Try this (see ?detach for more details):

detach("package:vegan", unload=TRUE)


It is possible to have multiple versions of a package loaded at once (for example, if you have a development version and a stable version in different libraries). To guarantee that all copies are detached, use this function.

detach_package <- function(pkg, character.only = FALSE)
{
  if(!character.only)
  {
    pkg <- deparse(substitute(pkg))
  }
  search_item <- paste("package", pkg, sep = ":")
  while(search_item %in% search())
  {
    detach(search_item, unload = TRUE, character.only = TRUE)
  }
}

Usage is, for example

detach_package(vegan)

or

detach_package("vegan", TRUE)

这篇关于如何在不重新启动 R 的情况下卸载包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆