Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

r - Looking at internal Methods

I'd like to be able to see the function that's used when I use str(), as I'd like to modify it a bit for my own purposes as another function.

When I type str(), I get the following:

function (object, ...) 
UseMethod("str")
<environment: namespace:utils>

So I tried, getAnywhere(str):

2 differing objects matching ‘str’ were found
in the following places
  .GlobalEnv
  package:utils
  namespace:utils
Use [] to view one of them

But there's nothing in the documentation about what the syntax should be for using []

So I tried, getAnywhere(str)[1]:

function (object, ...) 
UseMethod("str")
<environment: namespace:utils>

Sigh. Allright, what about showMethods(str):

Function "str":
 <not a generic function>

So, how do I see the construction of the output for str()? Or can I?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want methods() for an S3 generic such as str():

> methods(str)
[1] str.data.frame* str.Date*       str.default*   
[4] str.dendrogram* str.logLik*     str.POSIXt*    

   Non-visible functions are asterisked

Using getAnywhere(str) is not really helpful because str() is visible so you get the same result if you just run str at the prompt. You need getAnywhere() to look at the hidden methods listed above:

getAnywhere(str.default)

for example.

Shame you need to know what kind of generic a function is to list the methods; seems user-friendliness would be improved if R didn't care what kind of method type was supplied to one or other of these functions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...