refactored R code and added code to complete the generic solution for this tool to call R functions depending on the input