Reputation: 11
I have alot of different scripts in R that sources one another with source()
. Im looking for a way to create an overview diagram, that links each script visually, so i can easily see the "source hierarchy" of my code.
The result could look something like:
I hope there is a solution, that doesnt require a software license.
Hope it makes sence! :)
Upvotes: 0
Views: 1091
Reputation: 270075
For purposes of example change directory to an empty directory and run the code in the Note at the end to create some sample .R files.
In the first two lines of the code below we set the files
variable to be a character vector containing the paths to the R files of interest. We also set st
to the path to the main source file. Here it is a.R
but it can be changed appropriately.
The code first inserts the line contained in variable insert
at the beginning of each such file.
Then it instruments source
using the trace
command shown so that each time source
is run a log record is produced. We then source
the top level R file.
Finally we read in the log and use the igraph package to produce a tree of source files. (Any other package that can produce suitable graphics could be used instead.)
# change the next two lines of code appropriately.
# Settings shown are for the files generated in the Note at the end
# assuming they are in the current directory and no other R files are.
files <- Sys.glob("*.R")
st <- "a.R"
# inserts indicated line at top of each file unless already inserted
insert <- "this.file <- normalizePath(sys.frames()[[1]]$ofile)"
for(f in files)
inp <- readLines(f)
ok <- !any(grepl(insert, inp, fixed = TRUE)) # TRUE if insert not in f
if (ok) writeLines(c(insert, input), f)
}
# instrument source and run to produce log file
if (file.exists("log")) file.remove("log")
this.file <- "root"
trace(source, quote(cat("parent:", basename(this.file),
"file:", file, "\n", file = "log", append = TRUE)))
source(st) # assuming a.R is the top level program
untrace(source)
# read log and display graph
DF <- read.table("log")[c(2, 4)]
library(igraph)
g <- graph.data.frame(DF)
plot(g, layout = layout_as_tree(g))
For example, if we have the files generated in the Note at the end then the code above generates this diagram:
cat('
source("b.R")
source("c.R")
', file = "a.R")
cat("\n", file = "b.R")
cat("\n", file = "C.R")
Upvotes: 0
Reputation: 11
I can suggest you use Knime. it has the kind of diagram you are looking for. It has some scripts already wrote to clean, visualize data and write output and has integration with R and Python.
https://docs.knime.com/?category=integrations&release=2019-12 https://www.knime.com/
Good luck.
Upvotes: 1