Show all functions used in opened R script

Hi,
is there a way to show/list all functions used in R script with corresponding packages names ?
For example anova() and Anova() coming from two different packages.

I have used getAnywhere() but it returns only results for one particular function, I need sort of global solution.

This is not at all comprehensive, and very hacky, but might be OK if you just need to find functions which are called within the script using parentheses. With your script saved in script.R:

library(tidyverse)

random_script <- read_file("script.R")

strings_matching_open_paren_pattern <- random_script %>%
  str_extract_all("[:graph:]*\\(", simplify = FALSE) %>% # e.g. 'c(', 'data.frame('
  `[[`(1) %>%
  str_remove_all("\\(") %>%
  unique()

matched_functions <- strings_matching_open_paren_pattern %>%
  map(findFunction) %>%
  setNames(strings_matching_open_paren_pattern)

Assumptions:

  • All functions are called with open parentheses, so for example this won't pick up the + or %in% or [[ functions
  • No spaces between function name and open parenthesis (e.g. per tidyverse style guide)
  • All function names match the [:graph:]* regex and everything matching that regex is a function name

The result might end up containing some junk and, depending on how big your script is, you might want to finesse the matched_functions list a bit. Let me know if this helps!

It errors here:

setNames(strings_matching_open_paren_pattern) 
Error in exists(what, where, inherits = FALSE) : invalid first argument

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.