简体   繁体   English

加载一个功能包

[英]Load a package apart from one function

I would like to load an entire package apart from one function. 我想加载一个功能之外的整个程序包。

I see workarounds such as How to load only specific functions from a package but this does not fulfil my full needs. 我看到了一些变通方法,例如如何仅从程序包中加载特定功能,但这不能满足我的全部需求。

The specific problem is using loading the select() function within the MASS and tidyverse packages but I would like a more general solution. 具体的问题是使用在MASS和tidyverse程序包中加载select()函数,但是我想要一个更通用的解决方案。

Thank you. 谢谢。

There are three nice ways to do that, rising in difficulty: 有三种不错的方法可以做到,但难度却越来越大:

1. conflicted 1. conflicted

It checks for name conflicts and will prevent you from using masked or masking functions by throwing an error if you do. 它会检查名称冲突,并通过抛出错误来阻止您使用掩码或掩码函数。 But you can declare a session-wide preference, eg: 但是您可以声明整个会话的首选项,例如:

conflict_prefer("filter", "dplyr")
#> [conflicted] Will prefer dplyr::filter over any other package

conflicted on Github 在Github上发生冲突

2. import 2. import

It allows you to explicitly import specific functions from packages (and give them a custom name, if you like) 它允许您从包中显式导入特定功能(并根据需要给它们指定一个自定义名称)

import::from(ggplot2, g = ggplot, aes, geom_point)
g(iris, aes(Petal.Width, Petal.Length)) + geom_point()

import on Github 在Github上导入

3. modules 3. modules

It gives you a Python-esque way of importing both modules (written as R source files) and libraries and a more cohesive fashion. 它为您提供了一种Python风格的方式来导入模块(写为R源文件)和库,并且更具凝聚力。 The nice (but advanced) thing is that modules, if they have subgroups, can be loaded partially, eg str = import('tools/strings') . 不错(但很高级)的是,如果模块具有子组,则可以部分加载它们,例如str = import('tools/strings') This does not work for packages, however, as they are written as monoliths. 但是,这对于包装不起作用,因为它们以整体形式编写。 modules has some advantages coding guideline wise, but will force you to write: 模块在编码准则方面有一些优势,但是会迫使您编写:

dplyr = import_package('dplyr')
cars %>% dplyr$filter(speed > 15)

modules on Github Github上的模块

If you do select <- MASS::select you should be fine. 如果您select <- MASS::select ,应该没问题。

If you really don't want it in your global workspace you could do, after attaching MASS (optionally), and dplyr : 如果您确实不希望在全局工作区中使用它,则可以在附加MASS (可选)和dplyr

attach(list(select=MASS::select),name = "front_select")

This way it will find this one before the others because the environment front_select will be met first in the search path. 这样,它将在其他条件之前找到它,因为环境front_select将首先在搜索路径中得到满足。

That's not very orthodox though. 不过,这不是很正统。

This is assuming you want this for interactive use, if not by all means use :: notation. 这是假设您要将此内容用于交互式使用,如果不是一定要使用::表示法。


Actually what you're asking for is possible, though it's a lot of black wizardry, and I have a feeling I'll get downvoted for this, but this answers the question : 实际上,您要的是可能的,尽管这是很多黑巫术,而且我觉得我对此一无所获,但这可以回答问题:

library(dplyr)
x <- as.list(as.environment("package:dplyr"))
detach("package:dplyr")
x$select <- NULL
attach(x,name = "package:dplyr")
mutate
# function (.data, ...) 
# {
#     UseMethod("mutate")
# }
# <bytecode: 0x00000000190069c0>
# <environment: namespace:dplyr>

select
# Error: object 'select' not found

from ?search : 来自?search

Names starting "package:" are reserved for library and should not be used by end users. 以“ package:”开头的名称是为库保留的,最终用户不应使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM