Peter
Peter

Reputation: 132217

Get locals from calling namespace in R

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.)

Upvotes: 0

Views: 144

Answers (1)

Charles
Charles

Reputation: 4469

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

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

print(get('local_1'), parent.frame())

Upvotes: 2

Related Questions