The default print() method prints the source if available (from a srcref attribute) for expression objects, but not for calls. See below for an example (run in R 3.3.1).

It looks like this change has been introduced in R 2.10.0, in revision r48366 [2], but it hasn't been documented explicitly. I'm wondering if calls should use an attached srcref for printing. For now, deparse uses the class "srcref_call" with an overridden print() method.

Construct and parse a simple call

text <- "a(\n)"
text

ex <- parse(text = text, srcfile = srcfilecopy("dump.R", text))

Returns an "expression", it uses the srcref for printing

ex
attributes(ex)

Extract the call, attach srcref attributes

cl <- ex[[1]]
attr(cl, "srcref") <- attr(ex, "srcref")[[1]]
attr(cl, "srcfile") <- attr(ex, "srcfile")
attr(cl, "wholeSrcref") <- attr(ex, "wholeSrcref")

The call does not use the srcref for printing

cl

Hack around it

print.call <- function(x, ..., useSource = TRUE) {
  print(attr(x, "wholeSrcref"), ...)
}

Now works as expected

print(cl)


krlmlr/dp documentation built on May 20, 2019, 6:16 p.m.