forestgeo/AGBfluxes

Search and address TODO and FIXME

Opened this issue · 3 comments

I flagged a few things with TODO and FIXME that need to be addressed.

@ervanSTRI,

I noticed some issues that may make your code run much faster. In three places, the code grows objects instead of pre-allocating an object with the required structure (number of columns, rows, list elements, etc.) and then modifying each element in place. See my comments and let me know if it's unclear.

Generally, you can completely avoid this issue with lapply(). If you still need a for loop, then ensure to create an object with all the elements that will later be replaced (instead of created) inside the for loop.

Ok. I now see your code and comments. First question: why does the online version differ from the one I have in my package? I've seen the first occurrence you are referring to, but wonder if it saving a lot of time in filling vs cbind-ing a data-frame. I'll give it a try.

Lines 118, 675, and 845 in AGBfluxes_functions.R.

The source of receiving_df() is here. This is just a slightly different way to do what you were doing before. Notice that the dataframe it creates has only one row, when instead it should have as many rows as the object you will return. Maybe your need expand.grid() or similar.

I would start by fixing the receiving data-structure, and checking if it really improves how fast the code runs. If you achieve better performance, then transforming the code to lapply() (and fridns) won't make it faster -- only more readable. In short, the advantage of lapply() and friends are these:

  • It creates the receiving data-structure internally (so you don't have to do it and you avoid mistakes).
  • It is more readable.

To learn more about the transition from for loops to lapply() and friends see this 12' video: https://youtu.be/GyNqlOjhPCQ

These things are often better shown than explained. But I can't help much until I'm able to run your code and produce an output I can test against. Without that, any change I do may brake something inadvertently. Issue #20 is the priority.