I am trying to build a data frame like,
fun args uses_arg
1 myfun function (x, w=1) function (x, arg)
where column "fun" contains all the functions in the chosen environment, and args the function arguments. I would like to have the info in a dataframe for further filtering. Consider the following code.
myfun <- function(x, w=1) return(x * w)
lsf.str()
# myfun : function (x, w) # info from lsf.str().
# uses_arg : function (x, arg)
lsf_text <- lsf.str()
class(lsf_text)
# [1] "ls_str" # class.
#
print.default(lsf_text) # print as is.
# [1] "myfun"
# attr(,"envir")
# <environment: R_GlobalEnv>
# attr(,"mode")
# [1] "function"
#
# print class
print(lsf_text)
# myfun : function (x, w) # <<<<<<<<<<<
# uses_arg : function (x, arg)# <<<<<<<<<<<
#-------------------------------------------
You could do sth like
> fnm <- lsf.str()
> res <- list(fun=as.list(fnm[seq_along(fnm)]),
+ args=lapply(fnm, \(x) names(formals(get(x, .GlobalEnv)))),
+ param=lapply(fnm, \(x) gsub(r'{.*(?<=\()(.*)(?=\)).*}', '\\1',
+ deparse(args(get(x, .GlobalEnv)))[[1]],
+ perl=TRUE) |> strsplit(', '))) |>
+ list2DF()
> res
fun args param
1 myfun1 x, w x, w = 1
2 myfun2 x, z, w x, z, w
where
> str(res)
'data.frame': 2 obs. of 3 variables:
$ fun :List of 2
..$ : chr "myfun1"
..$ : chr "myfun2"
$ args :List of 2
..$ : chr "x" "w"
..$ : chr "x" "z" "w"
$ param:List of 2
..$ :List of 1
.. ..$ : chr "x" "w = 1"
..$ :List of 1
.. ..$ : chr "x" "z" "w"
Note, that there's a utils:::print.ls_str
.
Data:
myfun1 <- function(x, w=1) return(x * w)
myfun2 <- function(x, z, w) return(z*(x / w))