6

I am using mgcv package within my own package and so far everything worked nicely. However, I tried to introduce Tweedie family objects ("Tweedie()", "tw()") to my package, but I am getting an error when trying to use the latter.

The function is as simple as this (simplified here):

#' @importFrom mgcv tw
#'
.FamilyLink <- function(link) {
       familyObject <- tw(link = link)
       return(familyObject)
}

and I have this in NAMESPACE file

importFrom(mgcv,tw)

So after installing the package I call my function (non-exported, it's normally used within other function from my package)

fam <- MyPackage:::.FamilyLink("log")
fam$aic(1, 1, NULL, 1, 1)
Error in ldTweedie(y, mu, p = p, phi = scale) : 
  could not find function "ldTweedie"

Then I tried adding:

importFrom(mgcv,ldTweedie)
importFrom(mgcv,tw)

Installed the package and got the same problem. However if I load my package using devtools:

devtools::load_all("MyPackage")
fam <- .FamilyLink("log")
fam$aic(1, 1, NULL, 1, 1)
[1] 4.05723

everything works fine. So I was wondering am I doing something wrong here or how can I get my package/function to work simply by installing and not having to use load_all()? Thank you

  • I am not experienced with [R] packages, so this may be unexperienced question; but I am curious, why don't you simply call .FamilyLink(link="log") instead of .FamilyLink("tw", "log")? – MindaugasK Apr 02 '20 at 13:19
  • Thanks for comment. It's typo while I was simplifying the actual code. – Daniel Piacek Apr 02 '20 at 13:21
  • Maybe it is related with environments... https://stackoverflow.com/questions/9272685/how-to-get-r-script-file-name-when-a-function-in-it-is-called https://stackoverflow.com/questions/1815606/determine-path-of-the-executing-script http://adv-r.had.co.nz/Environments.html E.g. function mgcv::ldTweedie is not visible in myPackage:::.FamilyLink function environment... what about if you make .FamilyLink visible outside package? Sry just guessing what could be the reason :) – MindaugasK Apr 02 '20 at 14:37
  • I tried exporting the function, didn't work. – Daniel Piacek Apr 02 '20 at 17:16
  • I still would think the issue relates with environments. Have you ever dealt with them? Function tw() is in file "efam.r" of the mgcv package. And it has this command: env <- new.env(parent = .GlobalEnv). In fact, seems like all functions in this file have this statement. While functions ldTweedie, Tweedie are in the file "gam.fit3.r", and in this file there is only a single function containing env <- new.env(), and this function is negbin(). Perhaps changing env <- .GlobalEnv could solve the issue. Though don't really know how this can affect the calculations done in the package. – MindaugasK Apr 03 '20 at 10:00
  • On second thoughts, env <- .GlobalEnv most probably will not help... env <- parent.frame() probably not as well... Maybe env <- environment() would... – MindaugasK Apr 03 '20 at 13:21

1 Answers1

1

Not sure if this is the most elegant solution, but with the help from here R package - Transferring environment from imported package the solution is to re-export ldTweedie function from mgcv.

dplyr re-exporting %>%