inconsistent behaviour

Does anybody know why calls such as behave differently when called within a package compared to when the call is run natively.

For example, if I define a function:

get_call <- function() print(

And then call that from within a function, say:

run_me <- function(){
   y <- get_call()

The result when run within R will be:

> run_me()
y <- get_call()

However, if I create a package and put these functions inside it:

> mypkg:::run_me()

When ran from within a package, we lose the <- part of the output.

The output from returns a call with an attribute srcref when it is ran natively, but this doesn't appear when it is run from within a package. When it's printed, it calls print.srcref() which is why the <- appears, rather than calling the default printing function otherwise. But I don't understand why this srcref appears at all.

Changing get_call():

get_call <- function() print(attributes(

and re-running/re-installing gives

> run_me()
y <- get_call()


Is it different when the pkg exports the function like
mypkg::run_me() ?

I'm not a package guy, so this is speculation—it's the environment.

get_call <- function() print(
run_me <- function() y <- get_call()
#> function ()  
#>  - attr(*, "srcref")= 'srcref' int [1:8] 2 11 2 36 11 36 2 2
#>   ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x55a1afa86b48>

It's the same whether it's exported or not

If the package is loaded via devtools::load_all(), it still shows as the native version above. load_all() creates a kind of faux-namespace by loading all of the functions in the package directory into a new environment and then adds it on the search() path. Which is different to actually loading a package, where the package is built first. So there must be something within this building process that isn't affixing this srcref attribute, that does get attached when the function/call is built natively. Worth noting that sys.function() also gives an srcref.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.