如何阅读R函数的源代码? [英] How can I read the source code for an R function?

查看:129
本文介绍了如何阅读R函数的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,我想学习摘要如何生成它的信息.具体来说,汇总如何生成一个因子的每个级别中元素数量的计数.我可以使用摘要,但是我想学习如何更好地处理因素.当我尝试摘要时,我只是获得一般信息.因为它是字节码,所以这不可能吗?

I have a data frame and I want to learn how the summary generates it's information. Specifically, how does summary generate a count for the number of elements in each level of a factor. I can use summary, but I want to learn how to work with factors better. When I try ?summary, I just get the general info. Is this impossible because it is in bytecode?

推荐答案

键入summary时我们看到的是

> summary
function (object, ...) 
UseMethod("summary")
<bytecode: 0x0456f73c>
<environment: namespace:base>

这告诉我们summary是一个泛型函数,并且具有许多附加方法.要了解这些方法的实际名称,我们可以尝试

This is telling us that summary is a generic function and has many methods attached to it. To see what those methods are actually called we can try

> methods(summary)
 [1] summary.aov             summary.aovlist         summary.aspell*        
 [4] summary.connection      summary.data.frame      summary.Date           
 [7] summary.default         summary.ecdf*           summary.factor         
[10] summary.glm             summary.infl            summary.lm             
[13] summary.loess*          summary.manova          summary.matrix         
[16] summary.mlm             summary.nls*            summary.packageStatus* 
[19] summary.PDF_Dictionary* summary.PDF_Stream*     summary.POSIXct        
[22] summary.POSIXlt         summary.ppr*            summary.prcomp*        
[25] summary.princomp*       summary.srcfile         summary.srcref         
[28] summary.stepfun         summary.stl*            summary.table          
[31] summary.tukeysmooth*   

   Non-visible functions are asterisked

在这里,我们看到与summary函数关联的所有方法.这意味着在lm对象上调用摘要时与在data.frame上调用摘要时有不同的代码.这很好,因为我们不希望对这两个对象以相同的方式进行摘要.

Here we see all the methods associated with the summary function. What this means is that there is different code for when you call summary on an lm object than there is when you call summary on a data.frame. This is good because we wouldn't expect the summary to be conducted the same way for those two objects.

要查看在data.frame上调用摘要时运行的代码,只需键入

To see the code that is run when you call summary on a data.frame you can just type

summary.data.frame

,如方法列表中所示.您将能够对其进行检查和研究,并使用打印的代码做任何您想做的事情.您提到您对因素感兴趣,因此您可能希望检查summary.factor的输出.现在您可能会注意到,某些打印的方法在它们旁边有一个星号(*),表示它们不可见.从本质上讲,这意味着您不能只键入函数名称来尝试查看代码.

as shown in the methods list. You'll be able to examine it and study it and do whatever you want with the printed code. You mentioned that you were interested in factors so you will probably want to examine the output of summary.factor. Now you might notice that some of the methods printed had an asterisk (*) next to them which implies that they're non-visible. This essentially means that you can't just type the name of the function to try to view the code.

> summary.prcomp
Error: object 'summary.prcomp' not found

但是,如果确定要查看实际的代码,则可以使用getAnywhere函数进行查看.

However, if you're determined to see what the code actually is you can use the getAnywhere function to view it.

> getAnywhere(summary.prcomp)
A single object matching ‘summary.prcomp’ was found
It was found in the following places
  registered S3 method for summary from namespace stats
  namespace:stats
with value

function (object, ...) 
{
    vars <- object$sdev^2
    vars <- vars/sum(vars)
    importance <- rbind(`Standard deviation` = object$sdev, `Proportion of Variance` = round(vars, 
        5), `Cumulative Proportion` = round(cumsum(vars), 5))
    colnames(importance) <- colnames(object$rotation)
    object$importance <- importance
    class(object) <- "summary.prcomp"
    object
}
<bytecode: 0x03e15d54>
<environment: namespace:stats>

希望这可以帮助您将来更轻松地探索R中的代码.

Hopefully this helps you explore the code in R much more easily in the future.

有关更多详细信息,您可以查看《 R杂志》第6/4卷(警告,pdf ),并阅读Uwe Ligge的"R帮助台"部分,其中涉及查看R函数的源代码.

For even more details you can view Volume 6/4 of The R Journal (warning, pdf) and read Uwe Ligge's "R Help Desk" section which deals with viewing the source code of R functions.

这篇关于如何阅读R函数的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆