Liviu Andronic
2013-May-07 08:19 UTC
[R] recommended workflow for creating functions (was: Re: [Rd] Patch proposal for R style consistency (concerning deparse.c))
(moving to r-help) Dear all, I think Paul is raising a useful question here: What is the recommended workflow for creating a new function? R prides itself for letting users to create and use home-brewed functions: it's easy to maintain and re-use, doesn't clutter the global environment with intermediary objects, has clear input and output elements. All fine points, and I genuinely see the advantage in using functions, but after several years of using R I still stumble at step 1: building a function. The thing is that I'm not a whizz programmer, so it takes me a lot of time and mind gymnastics to imagine what the intermediary objects within a function, when executed, would look like. Thus I need to see and examine most intermediary objects while building the function. The alternative is to create the input objects and use only the first element while building the function, but this clutters the global workspace and I need to worry about overwriting existing objects. Another way would be to use browser() within the function, but this seems clunky to me. How do you deal with this? Is there a neat way to build a new function in a "sandbox", where I can access all objects in the global workspace but cannot overwrite them? Am I approaching this in the wrong way? Opinions welcome. Regards, Liviu On Sun, May 5, 2013 at 7:37 PM, Paul Johnson <pauljohn32 at gmail.com> wrote:> It quite often happens that when I'm developing a function, I have to step > through line by line to see what's going on. That' won't work for me if I > have "else" by itself at the beginning of a line. > > As an Objective-C programmer, I very much think it looks nicer to write > > if ( ) > { > blah > } > else > { > blah > } > > But in R, I can't step through that, so I don't write it that way. > > The fact that you don't run into that makes me think that I'm preparing > functions incorrectly. If you don't check your code line by line, what is > your work flow? >