Added option handling and writes R code to file