rnamespacesldacranmass

'MASS::predict.lda' is not an exported object from 'namespace:MASS'


This R code:

Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),
                   Sp = rep(c("s","c","v"), rep(50,3)))
train <- sample(1:150, 75)
z <- MASS::lda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train)
MASS::predict.lda(z)

gives the following error message:

Error: 'predict.lda' is not an exported object from 'namespace:MASS'

The predict.lda function of MASS is documented but, apparently, not part of the package's namespace. Why not?

This problem is important because I need to use predict.lda in a package of my own and this error is making it fail the CRAN checks.


Solution

  • We can load the package and then use predict

    library(MASS)
    predict(z)
    

    Or specify the :::. According to ?":::"

    Accessing exported and internal variables, i.e. R objects (including lazy loaded data sets) in a namespace.

    MASS:::predict.lda(z)
    #$class
    # [1] v s s s s c s v s v v v v c v v c v c s s s s c c v c v v c s s v c s s c v s c v v s c s c s c c s v c s s c s s c c c s c s v
    #[65] v v v s c s c v v s s
    #Levels: c s v
    
    #$posterior
    #               c            s            v
    #107 3.513603e-03 1.352029e-37 9.964864e-01
    #37  2.749629e-26 1.000000e+00 5.088976e-50
    # ...
    

    Or another option is to get the function from name space

    predictlda <- getFromNamespace("predict.lda", "MASS")
    predictlda(z)
    #$class
    # [1] v s s s s c s v s v v v v c v v c v c s s s s c c v c v v c s s v c s s c v s c v v s c s c s c c s v c s s c s s c c c s c s v
    #[65] v v v s c s c v v s s
    #Levels: c s v
    
    #$posterior
    #               c            s            v
    #107 3.513603e-03 1.352029e-37 9.964864e-01
    #37  2.749629e-26 1.000000e+00 5.088976e-50
    # ..