Is there a function in R that can tell me the attributes of a given object (or class)?
Consider the "dir" function in python when passed the file
class:
>>> dir(file)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__',
'__format__', '__getattribute__', '__hash__', '__init__', '__iter__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'close', 'closed',
'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name',
'newlines', 'next', 'read', 'readinto', 'readline', 'readlines',
'seek', 'soft space', 'tell', 'truncate', 'write', 'writelines',
'xreadlines']
Maybe there is an equivalent of type
as well(?)
>>> type(1)
<type 'int'>
Basic R Syntax: The dir R function returns a character vector of file and/or folder names within a directory.
The dir() function in python is an in-built function used on an object to look at all the properties / attributes and methods of that object, without its values (if the values of the attributes are given).
Help() and dir(), are the two functions that are reachable from the python interpreter. Both functions are utilized for observing the combine dump of build-in-function. These created functions in python are truly helpful for the efficient observation of the built-in system.
dir() function in Python. dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object (say functions , modules, strings, lists, dictionaries etc.) Returns : dir() tries to return a valid list of attributes of the object it is called upon.
R makes several different object oriented systems available to you, so if you don't know what species of object you're dealing with, you'll first need to determine whether it is one of S3, S4, or RC. Use isS4(x)
and is(x, 'refClass')
for this. If it's not S4 and not RC, it's S3. See Hadley's Advanced R chapter on object oriented programming for more information.
For S3 and S4 objects there are several functions you need to call to get information equivalent to Python's dir
. All of these methods will require you to supply the name of the class of the object as an argument, which you can determine with the class
function.
For methods, use methods(class=class(x))
for S3 objects and showMethods(class=class(x))
for S4 objects. To reveal "attribute" names/values, use attributes(x)
for S3 objects and getSlots(class(x))
for S4 objects. Note, getSlots
will only show the slot names and types, not their values. To access the values, you'll have to use slot
, but these values should also print when you simply print the object to the console.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With