[Rd] Recursively parsing srcrefs

Hadley Wickham hadley at rice.edu
Thu May 12 19:02:59 CEST 2011


Hi all,

Is it possible to "recursively" parse srcrefs to match the recursive
structure of the underlying code?  I'm interested in this because it's
the final step in allowing functions to modify other functions while
preserving the original source. I've tried to make a bit of demo of
what I want to do below, and I think I may uncovered a bug in parse.

f <- function(x = T) {
  # This is a comment
  if (x)                  return(4)
  if (emergency_status()) return(T)
}

parsed_src <- parse(text = attr(f, "source"))
src_ref <- attr(parsed_src, "srcref")[[1]]

f_one <- parse(text = "{
  # This is a comment
  if (x)                  return(4)
  if (emergency_status()) return(T)
}")
attr(f_one, "srcref")[[1]]

f_inside <- parse(text = "
  # This is a comment
  if (x)                  return(4)
  if (emergency_status()) return(T)
")
f_inside # Displays incorrectly?
attr(f_inside, "srcref")[[1]]
# Should be:
#  # This is a comment
#  if (x)                  return(4)


Regards,

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-devel mailing list