Cache function in R