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

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

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

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

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

target="_blank" rel="nofollow" href="#ulink_ef7cf5df-5f36-536e-967d-c5a69ac7cb62">a This behaviour is caused by the dispatcher-function implementation of an object-oriented programming model. To understand how this works and what it means, we refer to Section 6.2 “S3 Objects” on page 122.

      3 2 The character type in R is what in most other languages would be called a “string.” In other words, that is text that – generally – will not allow much arithmetic.

      4 a See Chapter 7 “Tidy R with the Tidyverse” on page 161 and note that tibbles (the data-frame alternative in the tidyverse) do not coerce text to factors.

      5 3 If your programming experience is limited, this might seem a little confusing. It is best to accept this and read further. Then, after reading Section 5.1 “Environments in R” on page 110 have another look at this section.

      6 4 A direct link to this file (in zip-format) is here: http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip?c6f8f9a0a5f970e31538be5271051b3c.

      7 5 With R it is also possible to read files directly from the Internet by supplying the URL in to the function read.csv().

      5.1 Environments in R

      Environments can be thought of as a set of existing or declared objects (functions, variables, etc.). When we start R, it will create an environment before the command prompt is available to the user.

      The top-level environment is the R command prompt. This is the “global environment” and known as R_GlobalEnv and can be accessed as .GlobalEnv.

       global environment

       R_GlobalEnv

      As mentioned earlier, the function ls() shows which variables and functions are defined in the current environment. The function environment() will tell us which environment is the current one.

       environment()

      environment() # get the environment ## <environment: R_GlobalEnv> rm(list=ls()) # clear the environment ls() # list all objects ## character(0) a <- “a” f <- function (x) print(x) ls() # note that x is not part of.GlobalEnv ## [1] “a” “f”

      # f # Multiple actions and side effects to illustrate environments # Arguments: # x -- single type f <- function(x){ # define a local function g() within f() g <- function(y){ b <- “local variable in g()” print(” -- function g() -- “) print(environment()) print(ls()) print(paste(“b is”, b)) print(paste(“c is”, c)) } # actions in function f: a <<- ‘new value for a, also in Global_env’ x <- ‘new value for x’ b <- d # d is taken from the environment higher c <- “only defined in f(), not in g()” g(“parameter to g”) print(” -- function f() -- “) print(environment()) print(ls()) print(paste(“a is”, a)) print(paste(“b is”, b)) print(paste(“c is”, c)) print(paste(“x is”, x)) } # Test the function f(): b <- a <- c <- d <- pi rm(x) ## Warning in rm(x): object ‘x’ not found f(a) ## [1] “ -- function g() -- “ ## <environment: 0x557538bf9e28> ## [1] “b” “y” ## [1] “b is local variable in g()” ## [1] “c is only defined in f(), not in g()” ## [1] “ -- function f() -- “ ## <environment: 0x557536bcc808> ## [1] “b” “c” “g” “x” ## [1] “a is new value for a, also in Global_env” ## [1] “b is 3.14159265358979” ## [1] “c is only defined in f(), not in g()” ## [1] “x is new value for x” # Check the existence and values: a ## [1] “new value for a, also in Global_env” b ## [1] 0 c ## [1] 3.141593 x ## Error in eval(expr, envir, enclos): object ‘x’ not found

      Each function within a function or environment will create a new environment that has its own variables. Variable names can be the same but the local environment will always take precedence. A few things stand out in the example above.

       The variable a does not appear in the scope of the functions.

       However, a function can access a variable defined the level higher if it is not re-defined in the function itself (see what happens with the variable c: it is not defined in g(), so automatically R will search the environment above.

       The function g() can use the variable b without defining it or without it being passed as an argument. When it changes that variable, it can use that new value, but once we are back in the function f(), the old value is still there.

      First, a variable does not necessarily need to be declared,R will silently create it or even change it is type.

      x <- ‘Philippe’ rm(x) # make sure the definition is removed x # x is indeed not there (generates an error message) ## Error in eval(expr, envir, enclos): object ‘x’ not found x <- 2L # now x is created as a long integer x <- pi # coerced to double (real) x <- c(LETTERS[1:5]) # now it is a vector of strings

      This can, of course, lead to mistakes in our code: we do not have to declare variables, so we cannot group those declarations so that these error become obvious. This means that if there is a mistake, one might expect to see strange results that are hard to explain. In such case, debugging is not easy. However, this is quite unlikely to come into your way. Follow the rule that one function is never longer than half an A4 page and most likely this feature of R will save time instead of increasing debugging time.

      Next, one will expect that each variable has a scope.

      # f # Demonstrates the scope of variables f <- function() { a <- pi # define local variable print(a)

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