Package 'functiondepends'

Title: Find Functions and their Dependencies
Description: Find functions in an unstructured directory and explore their dependencies. Sourcing of R source files is performed without side-effects: from R scripts that have executable code and function definitions only functions are sourced.
Authors: Jakub Sobolewski [aut, cre]
Maintainer: Jakub Sobolewski <[email protected]>
License: MIT + file LICENSE
Version: 0.2.3
Built: 2024-11-04 04:04:36 UTC
Source: https://github.com/jakubsob/functiondepends

Help Index


Find dependencies

Description

This function finds function calls inside a function with given name. Be aware that any variable that has a name that overwrites a function name will be recognised as a function call.

Usage

find_dependencies(
  function_name,
  envir = .GlobalEnv,
  in_envir = TRUE,
  add_info = FALSE
)

Arguments

function_name

Character, name of function

envir

Environment in which to search for function. Deafult is .GlobalEnv

in_envir

Logical, whether to return only functions that are loaded into envir

add_info

Logical, whether to add list column with line numbers of given function call in function body and a list column with context of said calls. Default is FALSE.

Value

A tibble with columns: - Source: character, name of function called inside 'Target' - SourceRep: integer, number of times 'Source' is called - SourceNamespace: character, name of namespace from which the function comes, if a function is defined in multiple namespaces then it is a vector. If function is user defined 'Namespace' is NA. - SourcePosition: optional, integer list with positions of 'Source' calls in body - SouceContext: optional, character list with lines of code with calls of 'Source' - Target: character, name of inspected function - TargetInDegree: integer, number of function calls inside of function body


Functions in path

Description

Parses files in given path. It searches for functions and loads them. Is safe for use with scripts as it doesn't source the whole file, just functions. There are no side-effects to sourcing .R files.

Usage

find_functions(
  path,
  envir = new.env(),
  recursive = TRUE,
  separate_path = FALSE
)

Arguments

path

Character, path to folder

envir

Environment to source loaded functions into

recursive

Logical, whether to search files recursively

separate_path

Logical, whether to split path into hierarchy of directories. Produces multiple character columns with 'Level' prefix.

Value

A tibble with character columns indicating path to source files and names of functions defined in them.

Examples

path <- file.path(tempdir(), "find_functions_example")
dir.create(path, showWarnings = FALSE)
code <- "
add <- function(x, y) {
  x + y
}
add_one = function(x) {
  add(x, 1)
}
assign('add_two', function(x) {
  add(x, 2)
})
"
write(code, file.path(path, "code.R"))
find_functions(path)