# [R-pkg-devel] Class with functions as slots

Glenn Schultz glennmschultz at me.com
Thu Nov 19 02:35:15 CET 2015

```All,

I have the following class with slots as functions. For the analysis of MBS this is good set-up as it allows for a very clean and modular way of handling things.  My question is really geared to understanding what is happening in R.

Below is the function without sato = NULL R CMD throws a warning global variable not defined.  With SATO = NULL I do not get the warning.  Why only sato and not two and ten which are also inputs?

Glenn

# Bond Lab is a software application for the analysis of
# fixed income securities it provides a suite of applications
# in addition to standard fixed income analysis bond lab provides
# for the specific analysis of structured products residential mortgage backed securities,
# asset backed securities, and commerical mortgage backed securities
# Copyright (C) 2014 Glenn M Schultz, CFA

#' An S4 class whose slots are functions used to propogate
#' the mortgage rate used to motivate the prepayment model
#' @slot yr30 A function defining the 30-year mortgage rate as a function
#' of the 2- and 10-year swap rate
#' @slot yr15 A function defining the 15-year mortgage rate as a function
#' of the 2- and 10-year swap rate
#' @export MortgageRate

setClass("MortgageRate",
representation(
yr30 = "function",
yr15 = "function"))

#' A constructor function for the class Mortgage Rate
MortgageRate <- function(){
sato = NULL
new("MortgageRate",
yr30 = function(two = numeric(), ten = numeric(), sato = numeric()) {
2.25 + (.06 * two) + (.75 * ten) + sato},

yr15 = function(two = numeric(), ten = numeric()){
1.75 + (.06 * two) + (.75 * ten) + sato}
)}
```