0

I want to be able to retrieve the local variables from R from a called function. Can I do this? (Like building a debugger.) Example:

show_locals <- function()
  # put something in here that shows local_1 and its value

local_1 = 123
show_locals()  # inspect local variables with custom formatting

Note: I will eventually require show_locals to be in a different library.

Edit: I would like to also see the value of local_1.

(This is similar to my questions for the same in Ruby or Python.)

Community
  • 1
  • 1
Peter
  • 127,331
  • 53
  • 180
  • 211
  • Can this be of help? http://stackoverflow.com/questions/3186174/what-class-of-objects-are-in-the-environment-r – nico Jul 08 '11 at 23:02
  • @nico: it will likely involve this function, but that only provides local variables from the immediate context, not the calling context. – Peter Jul 08 '11 at 23:04

1 Answers1

2
show_locals <- function() ls(parent.frame())

Edit by Peter: then, to get the value, use

print(get('local_1'), parent.frame())
Peter
  • 127,331
  • 53
  • 180
  • 211
Charles
  • 4,389
  • 2
  • 16
  • 13