In R, how to get an object's name after it is sent to a function?

前端 未结 3 581
傲寒
傲寒 2020-11-22 09:37

I am looking for the reverse of get().

Given an object name, I wish to have the character string representing that object extracted directly from the ob

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 10:39

    Note that for print methods the behavior can be different.

    print.foo=function(x){ print(deparse(substitute(x))) }
    test = list(a=1, b=2)
    class(test)="foo"
    #this shows "test" as expected
    print(test)
    
    #this (just typing 'test' on the R command line)
    test
    #shows
    #"structure(list(a = 1, b = 2), .Names = c(\"a\", \"b\"), class = \"foo\")"
    

    Other comments I've seen on forums suggests that the last behavior is unavoidable. This is unfortunate if you are writing print methods for packages.

提交回复
热议问题