R dplyr 过滤器没有屏蔽基础过滤器? [英] R dplyr filter not masking base filter?

查看:35
本文介绍了R dplyr 过滤器没有屏蔽基础过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作时,我有一台运行 R 3.1.2 的 Windows 7 计算机.

At work, I have a Windows 7 computer running R 3.1.2.

我有一个名为 packages.R 的文件.在我的这个文件中,我有以下代码:

I have a file called packages.R. In my this file, I have the following code:

library(dplyr)
library(sqlutils)
library(RODBC)

我的 .Rprofile 包含一个名为 .First 的函数.

My .Rprofile contains a function called .First.

.First <- function() {
    source("R/packages.R")
}

当我加载 R 时,我得到以下输出:

When I load R, I get the following output:

Loading required package: roxygen2
Loading required package: stringr
Loading required package: DBI

Attaching package: 'dplyr'

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

如果你仔细看这个,你会发现统计数据的过滤器没有被屏蔽.

If you look at this carefully, you will see the filter from stats is not masked.

但是,如果我采用完全相同的设置,并注释掉 packages.R 中的 library(dplyr) 语句,请保存文件,然后重新启动 R,然后手动....就像手动输入一样....

But, if I take my exact same setup, and comment out the library(dplyr) statement in packages.R, save the file, and restart R and then manually . . . . as in type it in by hand . . . .

library(dplyr)

Attaching package: 'dplyr'

The following object is masked from 'package:stats':

    filter

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

现在,它屏蔽了 package::stats.

Now, it masks package::stats.

我不明白.我需要在这个项目中大量使用 dplyr 的 filter 命令,我不想输入 dplyr::filter 来使用它.有人可以帮助我软弱的头脑理解为什么会这样吗?我曾尝试在 RStudio 和 ESS 中启动 R,并且在两者中都得到了完全相同的行为.我还尝试将 dplyr 移动到 packages.R 文件的末尾,结果没有区别.我只想屏蔽 stats::filter.谢谢.

I don't get it. I need to use the filter command from dplyr a lot for this project and I don't want to type dplyr::filter in order to use it. Could someone please help my weak mind understand why this is behaving this way? I have tried starting R in RStudio and ESS, and I get the exact same behavior in both. I also tried moving dplyr to the end of the packages.R file, with no difference to the results. I just want to mask stats::filter. Thanks.

推荐答案

当你在 .RProfile 中加载库时,它们会在 R 启动过程的早期,在 stats包附上.另一种方式是,您在 stats 已经加载后附加 dplyr.您可以通过键入 ?Startup 了解 R 的启动过程.它说:

When you load libraries in .RProfile they get attached very early in the R startup process, before the stats package is attached. The other way, you're attaching dplyr after stats has already been loaded. You can learn about R's startup process by typing ?Startup. There it says:

请注意,当获取站点和用户配置文件时仅加载基本包,因此其他包中的对象需要通过例如引用utils::dump.frames 或在显式加载相关包之后.

Note that when the site and user profile files are sourced only the base package is loaded, so objects in other packages need to be referred to by e.g. utils::dump.frames or after explicitly loading the package concerned.

我已经看到 Hadley 出于这个原因建议不要在 .RProfile 中加载包,即包加载顺序的差异,尽管我个人对此没有强烈的感觉.

I've seen Hadley recommend against loading packages in .RProfile for this reason, i.e. the discrepancies in package loading order, although personally I don't have strong feelings about it.

一种可能的解决方案是在加载 dplyr 之前,简单地将 library(stats) 添加为脚本中的第一个库调用.

One possible solution is to simply add library(stats) as the very first library call in your script, before loading dplyr.

要在全球范围内避免此类问题的另一个(长期)选择是将您的工作流程从大量脚本集合"转换为一个或多个包.

Another (long term) option to avoid these sorts of issues more globally would be to transition your workflows from "a large collection of scripts" to one or more packages.

这篇关于R dplyr 过滤器没有屏蔽基础过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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