1

In a long-term project that uses R, I have a set of about 50 functions that I've defined which I use all the time, and another 10 or 15 that I use once in a while. Most of these are not functions I use directly; they're helper functions I've used to define the ones I use directly. When I load data objects from R data files, I don't always remember what I named the objects, so I execute ls() to see what I loaded. If I've loaded my source doe files, though, I have to visually sift through the output to fine the data objects, which is obviously inconvenient.

So I wrapped the functions into an R package. If I just load the package, then everything is fine. I can use the functions, but ls() only shows me the data objects.

However, I keep adding new functions. And I find that remaking the package is not entirely trivial, so I end up loading my functions from the source files anyway much of the time--until I get around to remaking the package.

I can of course automate the package construction process more than I have, or split my new functions out in separate source files. I could also split the functions into different packages--maybe that would help. I think I could make the helper functions available only via a closure over the ones I call directly--I haven't tried that--but then I can't use the helpers directly if I want to do so, and it seems like overkill if there's a simpler way. And that's what I'm wondering: whether there's an easy trick that I've missed--a way to make functions available for use, but make it easy to show me what data objects I have without listing the functions. If not, OK, but I thought it would be worth asking. Thanks.

These questions: hiding personal functions in r and remove all variables except functions provide very useful answers. What is the absolutely easiest way to accomplish what's described above?

Community
  • 1
  • 1
Mars
  • 8,689
  • 2
  • 42
  • 70
  • RStudio displays the workspace organized in data, values, and functions. If you haven't already done so, you should give it a try. There are lots of other benefits in using an IDE. – Roland Mar 29 '13 at 16:13
  • I'm guessing that since you don't think 'remaking' the package is trivial that you aren't using the devtools package. It makes it really easy to make modifications to a package, test them out, and reinstall the package. – Dason Mar 29 '13 at 16:31
  • Even without devtools, "remaking" a package isn't difficult. – Joshua Ulrich Mar 29 '13 at 17:21
  • @JoshuaUlrich No - but I find `document(); install()` trivial whereas the alternative, while not difficult, isn't necessarily trivial. Unless you make a script to do it - which isn't too bad - but still not as easy as the process with devtools IMO. – Dason Mar 29 '13 at 17:29
  • Thanks for all of the helpful suggestions (and duplicate answer references). Did think it would have been answered, but was unsuccessful searching for it. (David Heffernan in [hiding personal functions in R](http://stackoverflow.com/questions/4828094/hiding-personal-functions-in-r) expressed my situation.) Will try devtools and the other tricks suggested. – Mars Mar 30 '13 at 02:48
  • Also, @Roland, re RStudio--good idea. I've used it some and will explore it further. Usually use R.app. I guess I like the visual simplicity of the interface in R.app, and I keep getting caught off guard in RStudio when ^P doesn't recall the last command; that's just second nature to me when working at a prompt. You've pointed out a benefit that might outweigh the costs. – Mars Mar 30 '13 at 18:21
  • Spacedman's answer provides new information that's not explicit in the answers to previous questions. (@Matthew Plourde's answer is given in [remove all variables except functions](http://stackoverflow.com/questions/8305754/remove-all-variables-except-functions), though his version is very slightly clearer given the question I asked.) As a result, I suggest that this not be marked as a duplicate question. I've edited my question to include links to the previous questions, but have suggested there might be an easier method--which there is: Spacedman's. Not sure this is best way to proceed. – Mars Mar 30 '13 at 19:01

2 Answers2

10

lsf.str returns a vector of the functions in your namespace, so you could use this to remove those names from the output of ls.

setdiff(ls(), lsf.str())
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
3

Put the functions in a package-structured folder (ie in a subfolder called R) and use package:devtools. The only thing you need do is load_all("myfuncs") when you edit your R code. Everything is reloaded into R, doesn't mess up your ls() listings, and is easy to package and document if you need to take this further. But its not compulsory. Just load_all after editing.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • This is great! Perfect. Thank you @Spacedman for sending me immediately to the shortest of shortcuts. – Mars Mar 30 '13 at 18:37
  • I also added a suggested edit: A DESCRIPTION file is needed to make this work. – Mars Mar 30 '13 at 18:46