This approach works, but you could also use the SFRM_Materialize mode and calculate the entire result set in one go. That tends to be simpler. See, for example crosstab_hash() in contrib/tablefunc for an example.
FWIW, there are also some good examples of array handling in PL/R, e.g. pg_array_get_r() in pg_conversion.c
Thanks. That looks like less code and probably will be slightly more efficient.