The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 43

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

b is not in the scope of the function } # Define two variables a and b a <- 1 b <- 2 # Run the function and note that it knows both a and b. # For b it cannot find a local definition and hence # uses the definition of the higher level of scope. f() ## [1] 3.141593 ## [1] 2 # f() did not change the value of a in the environment that called f(): print(a) ## [1] 1

      This illustrates that the scoping model in R is dynamical scoping. This means that when a variable is used, that R in the first place will try to find it in the local scope, if that fails, it goes a step up and continues to do so until R ends up at the root level or finds a definition.

       dynamical scoping

      image Note – Dynamic scoping

      # Citation from the R documentation: # Copyright (C) 1997-8 The R Core Team open.account <- function(total) { list( deposit = function(amount) { if(amount <= ) stop(“Deposits must be positive!\n”) total <<- total + amount cat(amount,“deposited. Your balance is”, total, “\n\n”) }, withdraw = function(amount) { if(amount > total) stop(“You do not have that much money!\n”) total <<- total - amount cat(amount,“withdrawn. Your balance is”, total, “\n\n”) }, balance = function() { cat(“Your balance is”, total, “\n\n”) } ) } ross <- open.account(100) robert <- open.account(200) ross$withdraw(30) ## 30 withdrawn. Your balance is 70 ross$balance() ## Your balance is 70 robert$balance() ## Your balance is 200 ross$deposit(50) ## 50 deposited. Your balance is 120 ross$balance() ## Your balance is 120 try(ross$withdraw(500)) # no way.. ## Error in ross$withdraw(500) : You do not have that much money!

      This example is best understood by realizing that this can also be written with a declared value “total” at the top level of our object.

      image Warning – Dynamical scoping

      While dynamical scoping has its advantages when working interactively, it makes code more confusing, and harder to read and maintain. It is best to be very clear with what is intended (if an object has an attribute, then declare it).Also never use variables of a higher level, rather pass them to your function as a parameter, then it is clear what is intended.)

      In fact, a lot is going on in this short example and especially in the line total <<- total + amount. First, of all, open.account is defined as a function. That function has only one line of code in some sense. This is a fortiori the last line of code, and hence, this is what the function will return. So it will try to return a list of three functions: “deposit,” “withdraw” and “balance.”

      What might be confusing in the way this example is constructed is probably is that when the function open.account is invoked, it will immediately execute the first function of that list. Well, that is not what happens. What happens is that the open.account object gets a variable total, because it is passed as an argument. So, it will exist within the scope of that function.

      The odd thing is that rather than behaving as a function this construct behaves like a normal object that has an attribute amount and a creator function open.account(). This function in sets this attribute to be equal to the value passed to that function.

      image Hint –Write readable code

      R will store variable definitions in its memory, and it may happen that you manage to read in a file for example, but then keep a mistake in your file. If you do not notice the mistake, it will go unnoticed (since your values are still there and you can work with them). The next person who tries to reproduce your code will fail. Start each file that contains your code with:

      rm(list=ls()) # clear the environment

      At this point, it is important to get a better understanding of the OO model implemented in R – or rather the multitude of models that are implemented in R.

      1 1 To fully appreciate what is going on here, it is best to read the section on the object models (Chapter 6 “The Implementation of OO” on page 87) in R first and more especially Section 6.2 “S3 Objects” on page 91.

       OO

       object oriented

      Personally, I find it useful to think of R as a functional language with odd object possibilities. This means that if you want to make some simple analysis, then you might skip this section. We did our best to keep the knowledge of OO to a minimum for the whole book.

      Programming languages that provide support for object oriented programming, allow code to be data-centric as opposed to functional languages that are in essence logic-oriented. They do that by introducing the concept of a “class.” A manifestation of that class then becomes the object that we can work with. Objects represent real life things. For example, if we want to create a software that manages bank accounts, it might be possible to have one object that is the account, another that is the customer, etc.

      The main idea is that in other parts of the code we can work with the object “account” and ask that object

Скачать книгу