R List functions in file

2019-07-19 00:46发布

问题:

How do I list all functions of a certain R file doing something like

list = list.all.functions(file.name, alphabetical = TRUE, ...)

where list is a string vector containing the names of the functions in file.name?

The solution of How to list all the functions and their arguments in an R file? gives no output for me (since I am not interested in arguments I opened a new question).

EDIT

File allometry.R starts with

#==========================================================================================#
#==========================================================================================#
#    Standing volume of a tree.                                                            #
#------------------------------------------------------------------------------------------#
dbh2vol <<- function(hgt,dbh,ipft){
   vol  = pft$b1Vol[ipft] * hgt * dbh ^ pft$b2Vol[ipft]
   return(vol)
}#end function dbh2ca
#==========================================================================================#
#==========================================================================================#

My main looks like

rm(list=ls())

here     = "/directory/of/allometry.R/"
setwd(here)

is_function = function (expr) {
  if (! is_assign(expr))
    return(FALSE)
  value = expr[[3]]
  is.call(value) && as.character(value[[1]]) == 'function'
}

function_name = function (expr)
  as.character(expr[[2]])

is_assign = function (expr)
  is.call(expr) && as.character(expr[[1]]) %in% c('=', '<-', 'assign')


file_parsed = parse("allometry.R")
functions = Filter(is_function, file_parsed)
function_names = unlist(Map(function_name, functions))
标签: r ls