如何防止R加载程序包? [英] How can I prevent R from loading a package?

查看:78
本文介绍了如何防止R加载程序包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中使用多核软件包来并行化我的代码.但是,如果加载了tcltk软件包,则使用多核软件包的派生过程将导致R无限期挂起.因此,我想防止tcltk永远加载.如果任何程序包尝试将其作为依赖项加载,我都希望立即出现错误.这可能吗?

I am using the multicore package in R for parallelizing my code. However, if the tcltk package is loaded, forking processes with the multicore package will cause R to hang indefinitely. So I want to prevent tcltk from ever loading. I want an immediate error if any package tries to load it as a dependency. Is this possible?

或者,我可以在包裹装入后卸载吗?

Alternatively, can I unload a package after it has been loaded?

推荐答案

如果立即在连接后将其拆离是一个很好的解决方案,请尝试执行以下操作:

If immediately detaching the package after it's been attached is a good enough solution, then try something like the following:

setHook(hookName = packageEvent("tcltk", "attach"),
        value =  function(...) detach(package:tcltk))

# Try it out
library(tcltk)
# Loading Tcl/Tk interface ... done
# Error in as.environment(pos) : invalid 'pos' argument
search()
# [1] ".GlobalEnv"        "package:graphics"  "package:grDevices"
# [4] "package:utils"     "package:datasets"  "package:methods"  
# [7] "Autoloads"         "package:base"     

如果(看起来很可能)是加载&附上软件包会引起问题,您也可以采用一种策略,例如在问题注释中勾勒出的策略.即:

If (as seems likely) the very act of loading & attaching the package is causing the problem, you might also pursue a strategy like the one sketched out in the comments to your question. Namely:

  1. 创建一个无害的虚拟包,也称为 tcltk
  2. 将其放置在名为"C:/R/Library/dummy/"的目录中.
  3. 在运行任何其他命令之前,通过执行.libPaths(c("C:/R/Library/dummy/", .libPaths()))将该目录放在.libPaths之前.
  1. Create a harmless dummy package, also named tcltk
  2. Place it in a directory named, e.g., "C:/R/Library/dummy/".
  3. Before running any other commands, prepend that directory to .libPaths by executing .libPaths(c("C:/R/Library/dummy/", .libPaths())).

然后,如果有任何软件包尝试加载 tcltk ,它将首先在"C:/R/Library/dummy/"中查找软件包,并找到其中一个名称,将对其加载片刻(在立即分离之前)通过上述挂钩).

Then, if any package attempts to load tcltk, it will first look for packages in "C:/R/Library/dummy/", and, finding one of that name, will load it for a moment (before it's immediately detached by the hook described above).

这篇关于如何防止R加载程序包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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