rqdap

How to avoid class name conflict with a package "loaded via a namespace (and not attached)" (qdap & openssl)


Using the qdap::polarity() function can sometimes run into the error:

Error in derive_pubkey(key) : RAW() can only be applied to a 'raw', not a 'list'

I'm fairly certain this is due qdap's key class conflicting with the openssl package's key class (since derive_pubkey() from openssl is referenced in the error msg).

The error happens when openssl appears in the loaded via a namespace (and not attached): section of sessionInfo(), and it seems to throw off method dispatch for the key class and cause the error.

I've only been able to fix the error by restarting my R session. Is there a way to remove openssl's footprint from the session to fix this issue? Or is there another way to avoid this issue without restarting R?

recreation of error

> successful      <- qdap::polarity("testing")
> load_openssl_ns <- body(openssl:::print.key)
> fails           <- qdap::polarity("testing")
Error in derive_pubkey(key) : 
  RAW() can only be applied to a 'raw', not a 'list'

Solution

  • For a dirty fix run

    `[[.qdap_hash` <- `[[.data.frame`
    

    Checking...

    > qdap::polarity("test")
      all total.sentences total.words ave.polarity sd.polarity stan.mean.polarity
    1 all               1           1            0          NA                 NA
    > library(openssl)
    Warning message:
    package ‘openssl’ was built under R version 3.3.3 
    > qdap::polarity("test")
    Error in derive_pubkey(key) : 
      RAW() can only be applied to a 'raw', not a 'list'
    > `[[.qdap_hash` <- `[[.data.frame`
    > qdap::polarity("test")
      all total.sentences total.words ave.polarity sd.polarity stan.mean.polarity
    1 all               1           1            0          NA                 NA
    > 
    

    The offending line in polarity is words <- c(posneg, alter[[1]])

    The object alter gets created with alter_env which creates an object which has classes "qdap_hash", "key", ...

    qdap_hash doesn't have it's own '[[' method so it checks to see if key has a '[[' method which it typically doesn't. Once openssl gets loaded there is a [[ method for key so it uses that and gives the error since it isn't in the form expected. If we define our own method for qdap_hash that gets called before even attempting to use [[.key so we bypass the issue. The author of qdap has been informed of the issue and the possible fix.