Tlatwork
Tlatwork

Reputation: 1525

Find the used method for R wrapper functions

I am Looking at this Question: What does useMethod mean here?.

I am trying to do the same for XML::read_xml

read_xml
(ms <- methods("read_xml"))

Output is:

[1] read_xml.character*  read_xml.connection*
[3] read_xml.raw*        read_xml.response* 

How do i know which of the four to take? And more importantly how does the call use_methods("read_xml") decides?

Upvotes: 0

Views: 162

Answers (1)

iod
iod

Reputation: 7592

You don't need to take any of the four. When you call on read_xml, the function looks at the class of the first argument (x, in this case) (e.g., a character string, a connection), and calls the function for that class. Basically, when you do read_xml(x), the function calls read_xml.[class(x)](x).

If the argument has more than one class, UseMethods will cycle through all the classes, from first to last, until if finds one with a method. Some functions might also have a default method, which is used if no other, more specific method, is found. read_xml, as you can see, doesn't have one. If you try using read_xml with a first argument that is, say, numeric, you'll get this error from UseMethod:

Error in UseMethod("read_xml") : 
  no applicable method for 'read_xml' applied to an object of class "c('double', 'numeric')"

As noted in the question you linked to, you can see the code for the specific class functions by using getAnywhere.

Upvotes: 2

Related Questions