R List functions in file

空扰寡人 提交于 2019-12-24 01:44:31

问题


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))

来源:https://stackoverflow.com/questions/39815780/r-list-functions-in-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!