Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R equivalent to the Python function "dir"?

Tags:

r

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'>
like image 732
Sigve Karolius Avatar asked Mar 04 '15 14:03

Sigve Karolius


People also ask

What is dir function in R?

Basic R Syntax: The dir R function returns a character vector of file and/or folder names within a directory.

What is __ DIR __ python?

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

What are HELP () and dir () in python?

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.

What does the python dir () function show when we pass an object into it as a parameter?

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.


1 Answers

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.

like image 57
Matthew Plourde Avatar answered Sep 22 '22 01:09

Matthew Plourde