为R代码构建文件图 [英] Build a file diagram for an R code

查看:94
本文介绍了为R代码构建文件图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理其他人编写的相对较大的R代码。该代码没有说明文件,并且分为多个文件,每个文件可以包含一个或多个功能。原始作者没有使用Makefile,因此没有关于什么叫什么的文档。

I need to work on a relatively large R code written by someone else. The code has no documentation, and it's split into countless files, each one of which can contain one or more functions. The original author didn't use a Makefile, so there's no documentation on what's calling what.

第一步,我想构建树状图。我的意思是说一棵树,其根是主文件,内部节点是被调用的各种文件,叶子(终端节点)是不调用其他文件中定义的函数的文件。有没有一种方法可以自动执行此操作?图片输出会很棒,但即使是文本文件也可以。 R Studio解决方案也可以。

As a first step, I'd like to build a dendrogram. I mean a tree whose root is the main file, the interior nodes are the various files called, and the leaves (terminal nodes) are files which don't call functions defined in other files. Is there a way to do that automatically? A picture output would be great, but even a text file would do. R Studio solutions would also be ok.

编辑:显然,我的描述不够清楚,因此我添加了一个非常简单的(平凡,实际上)示例。假设我有4个源文件 main.r foo.r bar.r blargh.r ,它们都位于同一文件夹中(实际情况下,大约50个文件整齐地存储在同一个红色文件夹中,输入/输出文件)。 main.r 的内容是:

EDIT: apparently my description is not clear enough, thus I add a very simple (trivial, actually) example. Assume I have 4 source files main.r, foo.r, bar.r and blargh.r, all in the same folder (the real case has ~50 files all "tidily" stored in the same ruddy folder, together with input/output files). The content of main.r is:

# main.r
source("foo.r")
source("bar.r")
source("blargh.r")

foo()
bar()

foo.r

# foo.r
foo <- function(){
    print("I'm foo")
    blargh()
}

bar .r

# bar.r
bar <- function(){
    print("I'm bar")
    blargh()
}

blargh.r

# blargh.r
blargh <- function(){
    print("I'm blargh")
}

我需要生成的是这样的一个图:

What I need to generate is a diagram like this:

  main.r
   ¦--foo.r                 
   ¦   ¦       
   ¦   °--blargh.r       
   ¦       
   °--bar.r                 
       ¦    
       °--blargh.r          


推荐答案

假定路径包含所有子目录和文件,

Assuming that the path contains the all the sub-directories and files,

files <- list.files(path, full.names = TRUE, recursive = TRUE)
#for eg.
files<- c(
    "root/dir1/some/file1.R", 
    "root/dir1/another1/file2.R", 
    "root/dir1/another1/new/file3.R", 
    "root/dir2/some/data1.csv", 
    "root/dir2/more/data2.csv"
)

现在,所有文件都在 files 变量中。

Now you have all your files, in files variable.

library(data.tree)
library(plyr)

a <- lapply(strsplit(files, "/"), function(z) as.data.frame(t(z)))
a <- rbind.fill(a)
mytree <- data.tree::as.Node(data.frame(pathString = path))
plot(mytree)

1  root                  
2   ¦--dir1                 
3   ¦   ¦--some          
4   ¦   ¦   °--file1.R    
5   ¦   °--another1       
6   ¦       ¦--file2.R    
7   ¦       °--new      
8   ¦           °--file3.R
9   °--dir2                 
10      ¦--some          
11      ¦   °--data1.csv  
12      °--more          
13          °--data2.csv  

注意:此结构称为树状图
希望这会有所帮助。

Note : This structure is called a dendogram. Hope this helps.

这篇关于为R代码构建文件图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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