简体   繁体   中英

Loading dplyr via Rprofile.site in Rstudio

Since I've found it, I use dplyr religiously. Since I use it so much, I tried putting library(dplyr) in my .First() function in Rprofile.

That loads dplyr, but when I fire up Rstudio, some of dplyr's functions get masked. For example, when I try to do:

foo <- mtcars %>% filter( cyl == 4 )

I get the follow error:

Error in filter(mtcars, cyl == 4) : object 'cyl' not found

A bit of troubleshooting revealed that if I run library(dplyr) again in the console, the problem clears right up--clearly one/some of dplyr's functions are getting masked by something further down the search path (or up, depending on how you look at it.

When I look at my search() path, I see that there are a host of other packages being loaded after dplyr, which isn't really what I want (I'd prefer that dplyr was the last thing loaded, or at least nearly last):

[1] ".GlobalEnv"        "tools:rstudio"     "package:stats"     "package:graphics"  "package:grDevices" "package:utils"    
[7] "package:datasets"  "package:xlsx"      "package:xlsxjars"  "package:rJava"     "package:dplyr"     "package:methods"  
[13] "Autoloads"         "package:base" 

I'm on Windows 7, using RStudio v.0.98.1028. Because I'm really unclear how Windows + RStudio handles the home directory, I'm editing Rprofile in C:/Program Files/R/R-3.1.1/etc/ . Any suggestions?

In the startup order for R (see ?Startup ), .First() is called before .First.sys() which loads the other packages. It is the stats package that overwrites filter .

.First.sys() uses options("defaultPackages") to determine what to load, so I suggest you edit that in your .First() function with:

options(defaultPackages=c(getOption("defaultPackages"),"dplyr"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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