30

When I load my package into the global environment, I get the following message

> library(saber)

Attaching package: ‘saber’

The following objects are masked _by_ ‘.GlobalEnv’:

    load.schedule, teamStats

I don't know what that means, nor whether I should be concerned about it.

Why is this message being delivered, and what does it mean?

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245

6 Answers6

23

It means that you have objects (functions, usually) present in your global environment with the same name as (exported) things in your package. Type search() to see the order in which R resolves names.

The solution is to either,

  1. don't create objects with those names in your global environment
  2. rename the objects in your package to something that's less likely to create a conflict, or rethink your decision to export them, or
  3. remember that you will always have to refer to those objects as saber::teamStats.

Probably (2) is best, unless the circumstances that led to the message are truly unusual.

joran
  • 169,992
  • 32
  • 429
  • 468
10

There's a third implied question that I don't think has been fully answered for this particular case. How to fix it in the situation where an earlier version of your own function is stuck in the global environment and masking new versions you're trying to test?

Renaming your function with every rev is not practical in this situation. I had the same situation and found deleting the .Rdata file in the working directory before restarting R solved the problem.

This has happened to me only twice over hundreds of time assembling my packages. I'm still not sure how the functions are occasionally getting stuck in global.

JRT
  • 101
  • 1
  • 3
6

This means that you have objects named load.schedule, teamStats in your workspace as well as in the library you are loading. It is warning you that when you call load.schedule it will use the one in your workspace (since it is first in the search path) rather than the one you are attaching. Try for example

ddply <- function(x) x + 1
library(plyr)

# Attaching package: ‘plyr’
# 
# The following object is masked _by_ ‘.GlobalEnv’:
# 
#     ddply

ddply(3) # the one we just defined is used, as global env is first in the search path
#[1] 4
konvas
  • 14,126
  • 2
  • 40
  • 46
1

The answers above give the low-level causes.

I just thought it would be useful to point out I got that message when I had a project open in RStudio and I loaded the "same" library of that project.

With the explanations above its obvious doing this creates some kind of conflict.

bliswell
  • 91
  • 7
1

You can also use the conflicted package. https://cran.r-project.org/web/packages/conflicted/index.html

If you want saber to be used by default set: conflicted::conflict_prefer("kbl", winner = "saber")

If you want your objects to be used, set: conflicted::conflict_prefer("kbl", winner = ".GlobalEnv")

phargart
  • 655
  • 3
  • 14
0

Error shows in my R consoleThe reason is you used the above two variable as local variable in your Rconsole. Since it is global variable you need to clean your existing project if not necessary else rename the local variable

In my case:

I declared a time series as gas. Later During calling forecast package i encountered same error and i renamed the library and used the package