R check if function in a package was called from the package fun or externally

jangorecki

How can I check from within the package (exported) function if the current function execution has been called by the function from that package or it was called by external package / from global env.

My current approach, developed by experimenting:

myfun <- function(){
    current_call = sys.call()
    parent_call = sys.call(sys.parent())
    if(identical(current_call,parent_call) || !identical(environmentName(parent.env(environment(match.fun(parent_call[[1L]])))),"imports:mypkg")){
        cat("called externally\n")
    }
}

Seems to not handle anonymous functions constructed in the other packages which dependent on my package.
Not particularly devtools related but package development related which devtools address well.

Edit:
To goal is to invoke an action (cat() in above example) in any cases except the calls from my other functions within the same package.

GSee

Borrowing from data.table:::cedta, I put these two functions in a package called test

myfun <- function() {
  te <- topenv(parent.frame(1))
  if(isNamespace(te) && getNamespaceName(te) == "test") { # <-- "test" is the name of the package
    cat("called from my package\n")
  } else cat("Not called from my package\n")
}

tester <- function() {
  myfun()
}

After loading the test package, I get these results

test:::myfun()
#Not called from my package
test:::tester()
#called from my package

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R Package Development - point to objects called by function

From Dev

how do I change the color of output of a table created by using function called "datatable" from DT package in R?

From Dev

R function only fails when called via my package

From Dev

Output of hints function from hints package in R

From Dev

Exclude function from R package manual

From Dev

Choose function to load from an R package

From Dev

In R, how can I check for the existence of a function in an unloaded package?

From Dev

write a function that check whether a package has been install in R system

From Dev

Function to automatically check correct R package version is installed

From Dev

R Package development: overriding a function from one package with a function from another?

From Dev

R Calling internal function from an exported function inside package

From Dev

How to call an R function in a particular R package from Rcpp

From Dev

In python DEAP package, why a function can be called from a module which dose not have such function?

From Dev

Access and modify package functions called by .C() in R

From Dev

knitr ezspin there is no package called ‘R.oo’

From Dev

R: what's the proper way to overwrite a function from a package?

From Dev

How to import an R function from another package such that it would be available for the user?

From Dev

How to retrieve R function script from a specific package?

From Dev

How to override exported function from R package listed in Imports

From Dev

Using un-exported function from another R package?

From Dev

Function runs from R console, but fails when built into package

From Dev

Error with tex2docx function from reports R package

From Dev

For Loop for Event Study using the EvReturn Function from R Package "Erer"

From Dev

Getting arguments of heplot function from heplots R package

From Dev

Error return from the MODISSummaries function (R package MODISTools)

From Dev

MASE calculation using accuracy function from forecast package in r

From Dev

Using .Fortran() from R package with error saying function not available

From Dev

how to get model name from tbats function, forecast package in R

From Dev

R function to return the license of a package?

Related Related

  1. 1

    R Package Development - point to objects called by function

  2. 2

    how do I change the color of output of a table created by using function called "datatable" from DT package in R?

  3. 3

    R function only fails when called via my package

  4. 4

    Output of hints function from hints package in R

  5. 5

    Exclude function from R package manual

  6. 6

    Choose function to load from an R package

  7. 7

    In R, how can I check for the existence of a function in an unloaded package?

  8. 8

    write a function that check whether a package has been install in R system

  9. 9

    Function to automatically check correct R package version is installed

  10. 10

    R Package development: overriding a function from one package with a function from another?

  11. 11

    R Calling internal function from an exported function inside package

  12. 12

    How to call an R function in a particular R package from Rcpp

  13. 13

    In python DEAP package, why a function can be called from a module which dose not have such function?

  14. 14

    Access and modify package functions called by .C() in R

  15. 15

    knitr ezspin there is no package called ‘R.oo’

  16. 16

    R: what's the proper way to overwrite a function from a package?

  17. 17

    How to import an R function from another package such that it would be available for the user?

  18. 18

    How to retrieve R function script from a specific package?

  19. 19

    How to override exported function from R package listed in Imports

  20. 20

    Using un-exported function from another R package?

  21. 21

    Function runs from R console, but fails when built into package

  22. 22

    Error with tex2docx function from reports R package

  23. 23

    For Loop for Event Study using the EvReturn Function from R Package "Erer"

  24. 24

    Getting arguments of heplot function from heplots R package

  25. 25

    Error return from the MODISSummaries function (R package MODISTools)

  26. 26

    MASE calculation using accuracy function from forecast package in r

  27. 27

    Using .Fortran() from R package with error saying function not available

  28. 28

    how to get model name from tbats function, forecast package in R

  29. 29

    R function to return the license of a package?

HotTag

Archive