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

Add external dependencies #59

Open
RobertASmith opened this issue Apr 18, 2024 · 2 comments
Open

Add external dependencies #59

RobertASmith opened this issue Apr 18, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@RobertASmith
Copy link
Contributor

RobertASmith commented Apr 18, 2024

This is important because, as in the model and package developed for a client, a package may be used to run lots of the generic code (which can make up a high proportion of the model) and then what is left for the model visualisation is actually a smaller subset of model code.

Potential fix:

renv::dependencies() provides a table of dependencies by file path.

We can adapt and run by function! This will give external dependency list for each function.
Then, for set functions we could load the arguments and body. It won't be possible to load the full code with Roxygen because this may be stored somewhere else.

Note: we won't always want full function by function dependencies as this will be massive, in most cases we will just want a list of packages used.

@RobertASmith RobertASmith self-assigned this Apr 18, 2024
@RobertASmith RobertASmith added the enhancement New feature or request label May 7, 2024
@RobertASmith
Copy link
Contributor Author

RobertASmith commented Jul 3, 2024

This is an example script which can be run from R once you have cloned the package repo. It requires assertHE to be loaded.

rm(list = ls())

filename <- "tests/testthat/example_external_function/R/external_dependency.R"

df <- utils::getParseData(parse(filename, keep.source = TRUE),includeText = TRUE)

function_name <- assertHE::find_function_definitions(filename = filename)$text
source("tests/testthat/example_external_function/R/external_dependency.R")
df2 <- utils::getParseData(x = body(get(function_name)))

df2[df2$token == "SYMBOL_FUNCTION_CALL", ]

find("colMins")


assertHE::.called_by("external_dependency_NON_explicit")
parsed_output <- parse(filename, keep.source = TRUE)


find("colMins")

@RobertASmith
Copy link
Contributor Author

#' @title Identify the package of a function call
#'
#' @param id the id of the function call in the parsed code
#' @param df_ParsedData a dataframe of the parsed code
#'
#' @return
#' @export
#'
#' @examples
identify_SYMBOL_PACKAGE <- function(id, df_ParsedData){
  # get the parent of the function call
  parent_of_foo <- df_ParsedData$parent[which(df_ParsedData$id == id)]
  # get the package name if there is one
  package <- df_ParsedData$text[df_ParsedData$parent == parent_of_foo & df_ParsedData$token=="SYMBOL_PACKAGE"]
  # else find the package from environment
  if(length(package) == 0) package <- find(what = df_ParsedData$text[df_ParsedData$id == id])[1]
  
  # remove the text 'package:' from the object if it exists
  package <- gsub("package:", "", package)
  
  return(package)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant