在 R 中生成调用图

发布于 2024-10-14 05:03:21 字数 464 浏览 8 评论 0原文

我收到了大量格式不正确、包含大量函数的整体 R 代码,我想弄清楚哪些函数调用哪些函数。

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

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

编辑:基于 R 分析系统的解决方案很棒,假设您实际上可以运行代码...文件中的一半内容没有运行,我不知道它的作用...静态分析太多了我想,是为了希望。

编辑2:Roxygen 的调用图似乎做了静态分析,基于函数表达式的递归下降并检查is.callable。能够在任何函数上运行它真是太棒了......我明天可能会玩这个......

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.

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.

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

Edit: 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说好的呢 2024-10-21 05:03:22

我没有使用过它,但快速浏览一下 proftools包表明它可以做到这一点。您必须首先运行 Rprof,然后使用 proftools 来分析输出。我认为您需要 plotProfileCallGraph() 函数。

I have not used it, but a quick look at the proftools package indicates that it can do this. You'll have to run Rprof first and then use the proftools to analyze the output. I think you want the plotProfileCallGraph() function.

忆伤 2024-10-21 05:03:22

除了 proftools 和 profr 之外,R Wiki 上还有 Romain 的 Perl 脚本。与 graphviz 结合,它可以绘制图形(边缘加权)——请参阅此处< /a> 了解更多。

Besides proftools and profr, there is the Perl script by Romain on the R Wiki. Combined with graphviz, it does graphs (with edges weighted) -- see here for more.

青丝拂面 2024-10-21 05:03:21

profr 会帮助你吗?从文档中:

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

在此处输入图像描述

不完全是您想要的,但您可以根据您的需要进行调整。

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)

enter image description here

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

桃扇骨 2024-10-21 05:03:21

CodeDepends 包(CRAN网站 GitHub)看起来很有趣,但我还没有研究过。其中,它承诺

  • 在函数集之间创建调用图

大概通过 makeCallGraph() 函数

The CodeDepends package (CRAN, website, GitHub) looks interesting, I haven't looked into it though. Among others, it promises

  • creating call graphs between sets of functions

presumably through the makeCallGraph() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文