Courvoisier
Courvoisier

Reputation: 993

build package with overloaded operator

I'm new to building packages with R and I am starting with making a package that combines some functions that I wrote and often load independently. Among those functions there is an overloaded + operator for concatenating strings. It's simply:

`+` = function(x,y) {
    if(is.character(x) | is.character(y)) {
        return(paste(x , y, sep=""))
    } else {
        .Primitive("+")(x,y)
    }
}

I build the package in Rstudio and the package compiles and I can load it fine. But the + operator is missing from package when I load it. What am I missing?

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] TBKUseful_0.1.0

loaded via a namespace (and not attached):
[1] tools_3.3.2     withr_1.0.2     memoise_1.0.0   digest_0.6.12   devtools_1.12.0

Upvotes: 4

Views: 92

Answers (1)

Roland
Roland

Reputation: 132989

OP has said that they export functions using exportPattern("^[[:alpha:]]+"), i.e., specify functions to export using a regex. Well, that regex does not match "+":

grepl("^[[:alpha:]]+", "+")
#[1] FALSE

Writing R Extensions recommends the following to export everything not starting with a dot:

exportPattern("^[^\\.]")

Testing the regex:

grepl("^[^\\.]", "+")
#[1] TRUE

Note that using export for exporting specific functions is better practice.

Upvotes: 3

Related Questions