getSourceInfo: Which R files source other files

getSourceInfoR Documentation

Which R files source other files

Description

This function and its methods construct the graph matrix identifying which other R files each R file sources. This is useful when analyzing the scripts in a project to determine the relationships between them.

Usage

getSourceInfo(x, recursive = TRUE, ...)

Arguments

x

the R code to process. This cane be the name of a file, the parsed code, an expression.

recursive

a logical value. If TRUE, recursively process the files being sourced to see what files they source.

...

additional arguments passed to methods. Currently not used.

Value

a 2-column data.frame with each row containing the name of the R file that sources the file given in the second column.

If recursive is TRUE, getSourceInfo each file will have <n> rows in the result where <n> is the number of files that the file source()s. This will be 0 if the file A does not source() any other files.

If a file sourced by one file doesn't exist, we don't know if it source()s any other files and so add NA. This allows the caller to see all files that are intended to be source()d and then check which exist (rather than omitting these files from the result.)

Author(s)

Duncan Temple Lang

See Also

findCallsTo

Examples

f = system.file("sampleCode/source.R", package = "CodeAnalysis")
a = getSourceInfo(f, FALSE)
a

b = getSourceInfo(f)
b

## Not run: 
    # Project from git@github.com:mespe/Variety_trial_analysis.git
  ff = list.files("~/Book/ExploreCode/Variety_trial_analysis/code", pattern = "\.R$", full = TRUE)
  src = lapply(ff, getSourceInfo)
  names(src) = basename(ff)
  src = src[ sapply(src, nrow) > 0 ]

  library(igraph)
  tmp = src$temp_analysis
  tmp = tmp[!is.na(tmp$sourced), ]
  tmp$from = basename(tmp$from)
  g = graph_from_data_frame(tmp)
  plot(g)

  # All the files and what they source combined
  tmp2 = do.call(rbind, src)
  tmp2$from = basename(tmp2$from)
  tmp2 = tmp2[!is.na(tmp2$sourced), ]
  plot(graph_from_data_frame(tmp2))

## End(Not run)

duncantl/CodeAnalysis documentation built on Feb. 21, 2024, 10:49 p.m.