Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added filter argument in visualizeDependencies #176

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '6647340'
ValidationKey: '6845650'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'madrat: May All Data be Reproducible and Transparent (MADRaT) *'
version: 3.4.0
date-released: '2023-07-13'
version: 3.5.0
date-released: '2023-07-21'
abstract: Provides a framework which should improve reproducibility and transparency
in data processing. It provides functionality such as automatic meta data creation
and management, rudimentary quality management, data caching, work-flow management
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: madrat
Title: May All Data be Reproducible and Transparent (MADRaT) *
Version: 3.4.0
Date: 2023-07-13
Version: 3.5.0
Date: 2023-07-21
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", , "[email protected]", role = "aut"),
Expand Down
33 changes: 24 additions & 9 deletions R/visualizeDependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
#'
#' Creates a graphical visualization of dependencies between functions in the mr-universe.
#'
#' @param functions function or vector of functions to be analyzed
#' @param ... function(s) to be analyzed
#' @param direction Character string, either “in”, “out” or "both". If “in” all sources
#' feeding into the function are listed. If “out” consumer of the function are listed.
#' If “both” the union of "in" and "out" is returned.
#' @param order order of dependencies. Order 1 would be only functions directly called from (in case
#' of direction "in") or directly calling (in case of direction "out") are shown. Order 2 will also
#' show direct dependencies of the order 1 dependencies, order 3 also the direct dependencies from
#' order 2 dependencies, etc.
#' @param filter regular expression to describe elements which should be excluded from visualization
#' (e.g. "^tool" to exclude all tool functions)
#' @param packages packages to use when searching dependencies
#' @param filename If a filename is provided, the resulting graph will be saved
#' @author Debbora Leip
#' @author Debbora Leip, Jan Philipp Dietrich
#' @seealso \code{\link{getDependencies}}, \code{\link{getMadratGraph}}, \code{\link{getMadratInfo}}
#' @importFrom igraph make_ego_graph ego V V<- union
#' @export


visualizeDependencies <- function(functions, direction = "both", order = 2, #nolint
visualizeDependencies <- function(..., direction = "both", order = 2, filter = NULL, # nolint: cyclocomp_linter
packages = getConfig("packages"), filename = NULL) {

# check for required packages
if (!requireNamespace("graphics", quietly = TRUE)) {
stop("Package \`graphics\` required to visualize dependencies")
Expand All @@ -29,6 +30,8 @@ visualizeDependencies <- function(functions, direction = "both", order = 2, #nol
stop("Package \`grDevices\` required to save dependency graph")
}

functions <- c(...)

# get next order of neighbors
nextOrder <- function(graph, nodes, mode) {
egoGraph <- make_ego_graph(graph, nodes, order = 1, mode = mode)
Expand All @@ -40,8 +43,9 @@ visualizeDependencies <- function(functions, direction = "both", order = 2, #nol
combineGraphs1 <- function(graphs) {
graph <- graphs[[1]]
if (length(graphs) > 1) {
for (i in 2:length(graphs))
for (i in 2:length(graphs)) {
graph <- igraph::union(graph, graphs[[i]])
}
}
return(graph)
}
Expand All @@ -61,7 +65,6 @@ visualizeDependencies <- function(functions, direction = "both", order = 2, #nol

# find dependencies up to given order in both directions
for (func in functions) {

# direct neighbors incoming
current <- nextOrder(graphMadrat, func, mode)
graphTmp <- current[[1]][[1]]
Expand All @@ -87,6 +90,18 @@ visualizeDependencies <- function(functions, direction = "both", order = 2, #nol

# get full graph
dfGraphMadrat <- getMadratGraph(packages = packages)

if (!is.null(filter)) {
# exclude all entries which match the filter, but exclude target functions
# from filter
.filter <- function(x, filter, f) {
functionFilter <- paste0("^(", paste(f, collapse = "|"), ")$")
return(!grepl(filter, x) | grepl(functionFilter, x))
}
dfGraphMadrat <- dfGraphMadrat[.filter(dfGraphMadrat$to, filter, functions) &
.filter(dfGraphMadrat$from, filter, functions), ]
}

graphMadrat <- graph_from_data_frame(dfGraphMadrat)

# functions as list
Expand Down Expand Up @@ -121,8 +136,8 @@ visualizeDependencies <- function(functions, direction = "both", order = 2, #nol
# add corresponding mr package to each vertex of graph
for (i in seq_along(V(fullGraph))) {
V(fullGraph)$color[i] <- ifelse(V(fullGraph)$name[i] %in% dfGraphMadrat$from,
dfGraphMadrat$from_package[which(dfGraphMadrat$from == V(fullGraph)$name[i])[1]],
dfGraphMadrat$to_package[which(dfGraphMadrat$to == V(fullGraph)$name[i])[1]])
dfGraphMadrat$from_package[which(dfGraphMadrat$from == V(fullGraph)$name[i])[1]],
dfGraphMadrat$to_package[which(dfGraphMadrat$to == V(fullGraph)$name[i])[1]])
}

# all packages that are relevant
Expand All @@ -144,7 +159,7 @@ visualizeDependencies <- function(functions, direction = "both", order = 2, #nol
if (!is.null(filename)) grDevices::png(filename, 800, 800)
plot(fullGraph)
graphics::legend("topright", legend = c(pkg, "Central functions"), pch = 16,
col = c(colorsIn[seq_along(pkg)], "#c93535"), bty = "n")
col = c(colorsIn[seq_along(pkg)], "#c93535"), bty = "n")
if (direction == "both") graphics::legend("bottomright", legend = c("In", "Out"), pch = c(22, 21), bty = "n")
if (direction == "in") graphics::legend("bottomright", legend = c("In"), pch = 22, bty = "n")
if (direction == "out") graphics::legend("bottomright", legend = c("Out"), pch = 21, bty = "n")
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# May All Data be Reproducible and Transparent (MADRaT) *

R package **madrat**, version **3.4.0**
R package **madrat**, version **3.5.0**

[![CRAN status](https://www.r-pkg.org/badges/version/madrat)](https://cran.r-project.org/package=madrat) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1115490.svg)](https://doi.org/10.5281/zenodo.1115490) [![R build status](https://github.com/pik-piam/madrat/workflows/check/badge.svg)](https://github.com/pik-piam/madrat/actions) [![codecov](https://codecov.io/gh/pik-piam/madrat/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/madrat) [![r-universe](https://pik-piam.r-universe.dev/badges/madrat)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -55,7 +55,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **madrat** in publications use:

Dietrich J, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Kreidenweis U, Klein D, Führlich P (2023). _madrat: May All Data be Reproducible and Transparent (MADRaT)_. doi: 10.5281/zenodo.1115490 (URL: https://doi.org/10.5281/zenodo.1115490), R package version 3.4.0, <URL: https://github.com/pik-piam/madrat>.
Dietrich J, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Kreidenweis U, Klein D, Führlich P (2023). _madrat: May All Data be Reproducible and Transparent (MADRaT)_. doi: 10.5281/zenodo.1115490 (URL: https://doi.org/10.5281/zenodo.1115490), R package version 3.5.0, <URL: https://github.com/pik-piam/madrat>.

A BibTeX entry for LaTeX users is

Expand All @@ -64,7 +64,7 @@ A BibTeX entry for LaTeX users is
title = {madrat: May All Data be Reproducible and Transparent (MADRaT)},
author = {Jan Philipp Dietrich and Lavinia Baumstark and Stephen Wirth and Anastasis Giannousakis and Renato Rodrigues and Benjamin Leon Bodirsky and Debbora Leip and Ulrich Kreidenweis and David Klein and Pascal Führlich},
year = {2023},
note = {R package version 3.4.0},
note = {R package version 3.5.0},
doi = {10.5281/zenodo.1115490},
url = {https://github.com/pik-piam/madrat},
}
Expand Down
10 changes: 7 additions & 3 deletions man/visualizeDependencies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading