问题描述
在工作中,我有一台运行R 3.1.2的Windows 7计算机。
我有一个名为packages.R的文件。在我的这个文件中,我有以下代码:
library(dplyr)
库(sqlutils)
库(RODBC)
我的.profile包含一个名为.First的函数。
.First< - function(){
source(R / packages.R)
}
当我加载R时,我得到以下输出:
<$加载所需的软件包:roxygen2
加载所需的软件包:stringr
加载所需的软件包:DBI
附加软件包:'dplyr'
以下对象从'package:base'屏蔽:
intersect,setdiff,setequal,union
如果仔细观察,您将看到统计信息的过滤器不被屏蔽。
但是,如果我把我的完全相同的设置,并注释在packages.R中的库(dplyr)语句,保存文件,然后重新启动R然后手动。 。 。 。如在手中类型。 。 。 。
库(dplyr)
附加包:'dplyr'
以下对象从'package:stats'屏蔽:
过滤器
以下对象从package:base中被屏蔽:
intersect ,setdiff,setequal,union
现在,它屏蔽了package :: stats。
我不明白我需要从dplyr使用过滤器命令这个项目很多,我不想键入dplyr :: filter以使用它。有人可以帮助我弱智的理解为什么这样做是这样吗?我已经尝试在RStudio和ESS中启动R,并且我在两者中得到完全相同的行为。我也尝试将dplyr移动到packages.R文件的末尾,与结果没有区别。我只是想屏蔽stats :: filter。谢谢。
当您在 .RProfile
中加载库时,在R启动过程中很早就在附加了 stats 包之前。另一方面,您已经加载了 stats 之后附加 dplyr 。我看到Hadley建议不要在 .RProfile
中加载软件包(因为包装加载顺序不一致),虽然我个人对此没有很大的感觉。 (R的启动过程的细节在启动
中描述。)
一个可能的解决方案是简单地添加 library(stats)
作为脚本中第一个库调用,然后加载 dplyr 。
另一个(长期)选项可以在全球范围内避免这些问题,将您的工作流程从大量脚本转换为一个或多个软件包。
At work, I have a Windows 7 computer running R 3.1.2.
I have a file called packages.R. In my this file, I have the following code:
library(dplyr)
library(sqlutils)
library(RODBC)
My .Rprofile contains a function called .First.
.First <- function() {
source("R/packages.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.
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
Now, it masks package::stats.
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.
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. I've seen Hadley recommend against loading packages in .RProfile
for this reason (discrepancies in package loading order), although personally I don't have strong feelings about it. (The details of R's startup process are described in ?Startup
.)
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滤波器不掩蔽滤波器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!