An example of a recursive udf written in R