Type: Package
Title: Visualise Function Dependencies
Version: 1.0.0
Description: Easily create graphs of the inter-relationships between functions in an environment.
License: MIT + file LICENSE
URL: https://lewinfox.com/foodwebr/
BugReports: https://github.com/lewinfox/foodwebr/issues
Imports: cli, crayon, codetools, DiagrammeR, glue, rlang, stringr, tidygraph
Suggests: testthat
Encoding: UTF-8
Language: en-GB
RoxygenNote: 7.3.3
NeedsCompilation: no
Packaged: 2025-09-23 04:44:26 UTC; lewin
Author: Lewin Appleton-Fox [aut, cre]
Maintainer: Lewin Appleton-Fox <lewin.a.f@gmail.com>
Repository: CRAN
Date/Publication: 2025-09-30 07:30:07 UTC

Convert a foodweb to a matrix

Description

This is equivalent to calling get_funmat() on x.

Usage

## S3 method for class 'foodweb'
as.matrix(x, rownames.force = NA, ...)

Arguments

x

A foodweb

rownames.force, ...

Ignored, only included for compatibility with S3 generic

Value

An n*n numeric matrix where n is the number of functions in the foodweb. Each element ⁠[i][j]⁠ will be 1 if function i calls function j, and 0 otherwise.


Convert a foodweb to a tidygraph

Description

This is an S3 method for the generic tidygraph::as_tbl_graph().

Usage

## S3 method for class 'foodweb'
as_tbl_graph(x, ...)

Arguments

x

A foodweb object created by foodweb().

Value

A new tidygraph::tbl_graph object.


Filter a function matrix

Description

Filter a function matrix

Usage

filter_matrix(fn_name, fn_mat)

Arguments

fn_name

String giving the name of the function we're interested in

fn_mat

Matrix produced by foodweb_matrix()

Value

A filtered function matrix containing only functions that are direct descendants or antecedents of fn_name.


Create a foodweb

Description

A foodweb object describes the relationship of functions in an environment. It has two components: funmat (function matrix) which encodes the caller/callee relationships (i.e. which functions call which) and graphviz_spec which is a text representation of the graph and is used for the default plotting behaviour.

Usage

foodweb(
  FUN = NULL,
  env = parent.frame(),
  filter = !is.null(FUN),
  as.text = FALSE
)

Arguments

FUN

A function.

env

An environment, parent.frame() by default. Ignored if FUN is not NULL.

filter

Boolean. If TRUE, only functions that are direct descendants or antecedents of FUN will be shown.

as.text

Boolean. If TRUE, rather than rendering the graph the intermediate graphviz specification is returned.

Details

foodweb() looks at the global environment by default. If you want to look at another environment you can either pass a function to the FUN argument of foodweb() or pass an environment to the env argument. If FUN is provided then the value of env is ignored, and the environment of FUN will be used.

Value

If as.text is TRUE, a character vector. Otherwise, a foodweb object as described above.

Examples

# Create some functions to look at
f <- function() 1
g <- function() f()
h <- function() {
  f()
  g()
}
i <- function() {
  f()
  g()
  h()
}
j <- function() j()

x <- foodweb()
x

# You can access the components directly or via getter functions
x$funmat
get_graphviz_spec(x)

# Calculate the foodweb of a function in another package
foodweb(glue::glue)

Create a function caller/callee matrix

Description

Returns a matrix of 0s and 1s with a row and column for each function in an environment, such that if the function on the x-axis calls the function on the y-axis, the element is 1, otherwise 0.

Usage

foodweb_matrix(env = parent.frame())

Arguments

env

Environment in which to search for functions.

Value

An n x n matrix where n is the number of functions in env.


Utility function to pluralise "edge" or "edges"

Description

Used when generating descriptions of a foodweb.

Usage

foodweb_summarise(x)

Arguments

x

A foodweb

Value

A character string in the format "x nodes and y edge/s"


Which functions does a function call?

Description

Given an input function fn_name and a list of candidate functions funs_to_match, return a list of all the functions in funs_to_match that appear in the definition of fn_name.

Usage

functions_called_by(fn_name, funs_to_match, where)

Arguments

fn_name

⁠<chr>⁠
The name of the function of interest

funs_to_match

⁠<chr>⁠
Only these functions will be considered as parents

where

⁠<env>⁠
An environment, or text specifying an environment

Value

A character vector listing the functions in funs_to_match that call fn_name.


Extract the function matrix from a foodweb object.

Description

Extract the function matrix from a foodweb object.

Usage

get_funmat(x)

Arguments

x

A foodweb

Value

x$funmat - a numeric matrix.


Extract the GraphViz specification from a foodweb object.

Description

Extract the GraphViz specification from a foodweb object.

Usage

get_graphviz_spec(x)

Arguments

x

A foodweb

Value

x$graphviz_spec - a character scalar.


Create a graphviz specification from a function matrix

Description

Given a function matrix created by foodweb_matrix(), convert it into a text specification that can be passed to DiagrammeR::grViz().

Usage

graphviz_spec_from_matrix(funmat)

Arguments

funmat

A function matrix generated by foodweb_matrix().

Value

A text string.

See Also

graphviz.org/

Examples


fm <- matrix(c(0, 1, 1, 1, 0, 1, 0, 1, 0), nrow = 3)
colnames(fm) <- rownames(fm) <- c("foo", "bar", "baz")
graphviz_spec_from_matrix(fm)

Is an object a foodweb?

Description

Is an object a foodweb?

Usage

is.foodweb(x)

Arguments

x

The object to test

Value

Boolean


Create a new foodweb object

Description

A foodweb object describes the relationship of functions in an environment. It has two components: a funmat (function matrix) which encodes the caller/callee relationships (i.e. which functions call which) and a grviz_spec which is a text representation of the graph and is used for the default plotting behaviour.

Usage

new_foodweb(funmat)

Arguments

funmat

A function matrix created by foodweb_matrix()

Details

This function should not be called directly, use foodweb() instead.

Value

A foodweb.

See Also

foodweb


Plot a foodweb object

Description

Calls DiagrammeR::grViz() on the graphvis_spec element of the foodweb.

Usage

## S3 method for class 'foodweb'
plot(x, ...)

Arguments

x

A foodweb object.

...

Further arguments to be passed to DiagrammeR::grViz

Value

An object of class htmlwidget; the output is just that of the underlying call to DiagrammeR::grViz.


Print a foodweb object

Description

Prints the graphvis_spec member of a foodweb object.

Usage

## S3 method for class 'foodweb'
print(x, ...)

Arguments

x

A foodweb object.

...

Unused, only included for consistency with S3 generic.

Value

NULL, only called for the side effect of printing to the console.


Print a foodweb_matrix

Description

Print a foodweb_matrix

Usage

## S3 method for class 'foodweb_matrix'
print(x, ...)

Arguments

x

A foodweb_matrix

...

Unused

Value

x, invisibly