在R中生成调用图 [英] Generating a Call Graph in R

查看:118
本文介绍了在R中生成调用图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了一大堆格式不佳的整体R代码,其中包含很多功能,我想弄清楚什么功能称为什么功能。

I've been given a big chunk of poorly formatted monolithic R code with plenty of functions, and I'd like to work out what functions call what functions.

我以为我可以使用roxygen的@callGraph东西,但是a)代码需要放在一个包中,这对这段代码来说是一件令人头疼的事情,b)当我在我的一个简单的程序包上运行它时,它甚至似乎都不起作用。我看到一位Roxygen的作者发帖说,由于Rgraphviz的依赖性,调用图的生成被禁用了,但是代码在那里。

I thought I could use roxygen's @callGraph stuff, but a) the code needs to be in a package, which will be a headache with this code, and b) it doesn't even seem to work when I do run it on a simple package of mine. I see a posting from one of the Roxygen authors saying call graph generation is disabled because of the Rgraphviz dependency, but the code is there. Anyway.

任何人都有更好的方法来快速计算出foo调用bar,baz和qux,而qux调用quux吗?

Anyone got a better way of quickly working out that foo calls bar, baz, and qux, and that qux calls quux?

编辑:假设您可以实际运行代码,则基于R的配置文件系统的解决方案非常出色...文件中的一半内容都无法运行,我也不知道它的作用。

Solutions based on R's profiling system are great, assuming you can actually run the code... Half the stuff in the files doesn't get run, and I don't know what it does... Static analysis is too much to hope for, I guess.

编辑2:Roxygen的调用图东西似乎基于表达式的递归下降而进行了静态分析。该函数并检查是否可调用。能够在任何功能上运行它真是太好了……我明天可能会玩这个……

Edit 2: Roxygen's call graph stuff seems to do a static analysis, based on recursive descent of the expression of the function and checking for is.callable. It would be lovely to be able to run this on any function... I may play with this tomorrow...

推荐答案

profr 可以帮助您吗?从文档中:

Would profr help you out? From the documentation:

> ?profr
> glm_ex <- profr(example(glm))
Read 17 items
>      head(glm_ex)
             f level time start  end  leaf source
8      example     1 0.32  0.00 0.32 FALSE  utils
9  <Anonymous>     2 0.04  0.00 0.04 FALSE   <NA>
10      source     2 0.28  0.04 0.32 FALSE   base
11  prepare_Rd     3 0.02  0.00 0.02 FALSE   <NA>
12      render     3 0.02  0.02 0.04 FALSE   <NA>
13 getSrcLines     3 0.02  0.04 0.06 FALSE   base
>      summary(glm_ex)
               f          level             time          start       
 eval.with.vis  :10   Min.   : 1.000   Min.   :0.02   Min.   :0.0000  
 <Anonymous>    : 3   1st Qu.: 4.000   1st Qu.:0.02   1st Qu.:0.1200  
 lazyLoadDBfetch: 3   Median : 6.000   Median :0.02   Median :0.2000  
 %in%           : 3   Mean   : 7.211   Mean   :0.03   Mean   :0.1769  
 inherits       : 3   3rd Qu.: 9.000   3rd Qu.:0.02   3rd Qu.:0.2600  
 is.factor      : 3   Max.   :22.000   Max.   :0.32   Max.   :0.3000  
 (Other)        :65                                                   
      end            leaf            source         
 Min.   :0.0200   Mode :logical   Length:90         
 1st Qu.:0.1500   FALSE:75        Class :character  
 Median :0.2400   TRUE :15        Mode  :character  
 Mean   :0.2069   NA's :0                           
 3rd Qu.:0.2800                                     
 Max.   :0.3200                                     

> plot(glm_ex)

所追求的不尽如人意,但您可以根据需要进行调整。

Not quite what you are after, but you may be able to adapt it to your needs.

这篇关于在R中生成调用图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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