Im looking for a way to see what functions are in a .r file (like list.files). Used Google but couldnt find anything. Anybody a fix for this?
Asked
Active
Viewed 57 times
0
-
Is this just a .r file you have floating around or is it part of a package? – Tyler Rinker Jul 03 '15 at 15:20
-
1Are there only function definitions in your file? It's very hard to "see" what's in the file unless you actually evaluate the code in the file. Are there any consequences to evaluating the file? If would be nice if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jul 03 '15 at 15:20
-
@MrFlick I was thinking you could make an environment and then use the `local` arg to `source` to source the file. Then use `is.function` in an `ls` of that environment to find the functions. But right now the question is ambiguous and should be reproducible as you have asked. Also I think the **devtools** package might have a way to do this easier. – Tyler Rinker Jul 03 '15 at 15:23
-
1@TylerRinker Right. That's exactly what I would suggest. But of the file write to files or even deletes them, evaluating the R file could have undesired side effects. You could `parse()` the thing, but even then, it's pretty difficult to catch all the ways that functions can be created with evaluating. – MrFlick Jul 03 '15 at 15:26
-
if you use emacs ESS you can just open up the speedbar and it will parse the .R file showing a list of functions – Rorschach Jul 03 '15 at 15:30
-
@LegalizeIt Does it work with things like `assign("f", function(x) x+2)`? and `h<-function(x) x+5; g<-h`? Does it recognize both `f` and `g` as functions? – MrFlick Jul 03 '15 at 15:33
-
@MrFlick I just tested it. It only picks up `h` by default. I don't use it much, but it looks like you would need to customize it to get the others. – Rorschach Jul 03 '15 at 15:39
1 Answers
0
You can source
it into an environment, and then use a combination of ls
and is.function
.
e <- new.env()
source("file.R", e) # or sys.source()
# which are the functions?
ls(e)[sapply(e, is.function)]
# get function definitions
lapply(e, function(x) if(is.function(x)) x)

Hong Ooi
- 56,353
- 13
- 134
- 187
-
3
-
1This is potentially a very dangerous answer depending on what's in the file. This will execute all the code and then look to see what functions were created as a consequence. – MrFlick Jul 03 '15 at 15:44
-
-
@HongOoi No, it’s **not** the only way. There’s in fact no need to evaluate the source, it’s enough to parse it. – Konrad Rudolph Jul 03 '15 at 16:17
-
@KonradRudolph per the examples that MrFlick gave: `assign("f", function(x) x+2)` and `h<-function(x) x+5; g<-h` you have to evaluate the source. – Hong Ooi Jul 03 '15 at 16:19
-
@HongOoi Granted, but it’s questionable whether this is actually needed. For almost all purposes a static analysis is entirely sufficient. – Konrad Rudolph Jul 03 '15 at 16:21
-
@HongOoi [I’ve posted a solution](http://stackoverflow.com/a/31211194/1968) that will correctly treat your `assign` code, amongst other things. It *could* be extended to also treat the indirect function assignment. I’ve left that out. – Konrad Rudolph Jul 03 '15 at 16:41