Andrew
2009-Nov-18 10:35 UTC
[R] getting the name of a single object in R for debugging output
I often use a debug flag (set to TRUE) to turn on various debugging print statements in my R scripts. I was thinking I should create a function debugPrint(object,debugFlag), to print out the object name and contents if the debugFlag is set to TRUE. Then I wouldn't have to make my script ugly(..er) than it already is by adding IF statements all over the place. I've seen how ls() dumps object names, but how do I get access to the character representation of the name of an object. E.g. myVar<- 10 print(myVar) produces "10" I'd like to print out something like " myVar : 10" I'd appreciate any suggestions. Regards, Andrew [[alternative HTML version deleted]]
Henrique Dallazuanna
2009-Nov-18 11:10 UTC
[R] getting the name of a single object in R for debugging output
Try this: debugPrint <- function(x, ...){ print(sprintf("%s: %d", deparse(substitute(x)), x), ...) } On Wed, Nov 18, 2009 at 8:35 AM, Andrew <nodecorum at yahoo.com> wrote:> I often use a debug flag (set to TRUE) to turn on various debugging print statements in my R scripts.? I was thinking I should create a function debugPrint(object,debugFlag), > to print out the object name and contents if the debugFlag is set to TRUE.? Then I wouldn't have to make my script ugly(..er) than it already is by adding IF statements all over the place.? I've seen how ls() dumps object names, but how do I get access to the character representation of the name of an object. > > E.g. > > myVar<- 10 > > print(myVar) produces "10" > > I'd like to print out something like " myVar : 10" > > I'd appreciate any suggestions. > > Regards, > > Andrew > > > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Reasonably Related Threads
- randomly select duplicated entries
- Howto create variable from the name of another one and get content of it
- AGI: "Get variable" returns variable VALUE vs "Get full variable" returns variable NAME - bug or my misunderstanding?
- Variable assigned to grep output - missing letter n!
- Combining all possible values of variables into a new...