Loading library(dplyr) from .Rprofile results in error.

Hi,

I'm new in R and just started with R 3.6.2 and R Studio 1.2.5033 and get this error

Error in filter(.data = tmps, CN == "CL" & GTTF != "") : 
  unused argument (.data = tmps)

when I use "library(dplyr)" in my ".Rprofile".

When I don't use ".Rprofile" and manually run "library(dplyr)" it works.

The start-up message from RStudio (when loading dplyr) is different - missing filter, lag ...

Why?
I want to avoid always loading dplyr so I've used this solution.
Thanks

Details:

R_LIBS is set to "C:\TWS\Libs\R"
R_USER is set to L:\myname\Configs\R
I also had set R_PROFILE_USER but same problem.

Version 1: I have a .Rprofile within R_USER which only contains "library(dplyr)":

Start RStudio:

Attaching package: ‘dplyr’

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> # Read data file
> toto <- read.table(
+   file = paste0(Sys.getenv("TOTO_DATA_PATH"), "/Toto.csv"),
+   header = TRUE,
+   sep = "\t",
+   quote = "",
+   dec = "."
+ )
> tmps <- select(
+   .data = toto,
+   CN,GTTG,GTSO,GTTF
+ )
> head(tmps)
  CN GTTG GTSO GTTF
1 CI    4    H    R
2 CI    5    H    F
3 CI    4    A    F
4 CI    3    A    F
5 CI    4    A    R
6 CI    3    H    V
> tmpf <- filter(
+   .data = tmps,
+   CN == "CL" & GTTF != ""
+ )
Error in filter(.data = tmps, CN == "CL" & GTTF != "") : 
  unused argument (.data = tmps)
> 

Version 2: I have no .Rprofile within R_USER:

Start RStudio:

> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> # Read data file
> toto <- read.table(
+   file = paste0(Sys.getenv("TOTO_DATA_PATH"), "/Toto.csv"),
+   header = TRUE,
+   sep = "\t",
+   quote = "",
+   dec = "."
+ )
> tmps <- select(
+   .data = toto,
+   CN,GTTG,GTSO,GTTF
+ )
> head(tmps)
  CN GTTG GTSO GTTF
1 CI    4    H    R
2 CI    5    H    F
3 CI    4    A    F
4 CI    3    A    F
5 CI    4    A    R
6 CI    3    H    V
> tmpf <- filter(
+   .data = tmps,
+   CN == "CL" & GTTF != ""
+ )
> head(tmpf)
  CN GTTG GTSO GTTF
1 CL    4    A    F
2 CL    7    A    F
3 CL    4    H    F
4 CL    3    H    F
5 CL    4    H    F
6 CL    4    H    F

This happens because in first variant dplyr is loaded before stats and filter that you are using comes from stats, not from dplyr. In second variant it is reversed and filter comes from dplyr package.

You can either use qualified import with, e.g., dplyr::filter or take a look at conflicted package that allows you to specify explicitly which function should be used.

Personally, I would say that using dplyr::filter, dplyr::select etc. is preferable since it makes it explicit what is happening and where functions is coming from.

Thanks, but I'm wondering that the loading behavior is so different.

You can read more about the startup process here: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html.

The part where difference in your specific case comes from is this one: "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."

Yes - I've read all but as beginner who wants to run a little script in batch you oversee this. Finally the documentation what happens isn't really clearly represented.

Where is there a comment that a package "stats" is loaded with R (or R Studio?) when .Rprofile isn't used?

btw. I'm just started with a R course via Pluralsight and nobody mentioned about this problem..

Of course I will use qualified naming in the future. AND .. it would be a good idea "auto-overwriting" when loading a package would never happen. So you never - never - run into problems like this.

Thanks a lot for the fast help!

I would say that modifying .Rprofile is an advanced feature that you should use only when you understand what it actually does. In my ~5 years of using R I've never really had a good use-case for that and so far didn't have any problem because of that.

Anyways, good luck with R!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.