shadowtalker
shadowtalker

Reputation: 13853

R package scope and masking

I think this is a simple question, and the answer is probably somewhere in either Hadley's book or the official R documentation, or in the answers to Writing robust R code: namespaces, masking and using the `::` operator, but I haven't come across it yet (or don't remember reading about it).

Let's say I want to write a package that contains the function:

sort_by_column <- function(x, j) {
  j_ord <- order(x[, j])
  x[j_ord, ]
}

If I define this function in the global environment and I pass in a value for x that is a data.table, it will silently fail because [ will dispatch to [.data.table instead of [.data.frame:

library(data.table)
sort_by_column(iris, 3)
sort_by_column(data.table(iris), 3)

My gut tells me that [.data.table won't even be available to my package unless I explicitly import it, in which case this problem can't occur in a package the way it can occur in the global environment. Is that true? If not, how can I handle this kind of masking?

edit 2: Function sort_by_column is defined in package A. Another package, B was loaded before package A but is not explictly imported by A. Do calls from functions defined in A search in package B?

edit: To clarify, I want to define a function inside a package in such a way that it "ignores" other packages the user might have loaded, in order to avoid function naming conflicts like the one demonstrated above. Is this ignoring behavior automatic, or do I need to do something in particular?

Upvotes: 1

Views: 391

Answers (1)

IRTFM
IRTFM

Reputation: 263342

If you want to specify a particular method for "[" then you should be able to use:

 `[.data.frame`(x, TRUE, j)

Or test for data.tables using inherits and trap that as an edge case?

Upvotes: 2

Related Questions