[R] basic q re: parsing functions, declaration order

J Robertson-Burns robertsonburns at btinternet.com
Sun Apr 19 03:55:14 CEST 2015

On 18/04/2015 18:14, Gowder, Paul wrote:
> Hi there,
> So I’m doing some serious coding in R for the first time—writing a strategic simulation to run for a few (or many) thousand iterations,* and doing so in proper functional programming form,


>    write.table(results, file="simul_results.csv", row.names=TRUE, col.names=TRUE, sep=",”)
> # bonus question: there’s probably a vastly more efficient way to do this final write, any suggestions welcomed

Unless your results are massive, this is not worth
worrying about.

> }


> I’m just putting it all in one source file, which I plan to load using source(), and then actual execution will be via console input > simulate.strat(number of runs), leave town for the weekend, hopefully come back to find a csv with a bunch of results.
> But I’m not quite sure how the parser works with respect to defining all these functions.  Do I have to define them from inside out like one would in python?  (So, on the code above, that would mean defining, say, dist.goods() and dist.power() first, then outer.wrapper(), then simulate.strat().)  Or is there a way to prototype them like one would in C?  Or--- and I really hope this is the answer so I don’t have untangle the tangled web of functions I’m writing— is R smart enough/lazy enough to accept that the function defined at line K can call a function defined at line K+N and not stress about it?

Yes.  Functions just need to be defined at the
time they are used.

> thanks so much!  My google-fu is failing me on this one.
> -Paul
> * why on earth, you might ask, am I doing this in R, rather than C or something?  Because I have a ton of computing resources and a huge workload. CPU time is cheap and my time is expensive…

Correct.  Not a line of thinking that you are
likely to need to defend among this crowd.

An answer to the question that you didn't ask is:


That question is:  Where do I look when I discover
it isn't doing what I intend it to do?


> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list