Typescript Nested Functions In R