43
votes

Through learning R, I just came across the following code explained here.

open.account <- function(total) {
  list(
    deposit = function(amount) {
      if(amount <= 0)
        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 don't 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)
ross$balance()
robert$balance()

ross$deposit(50)
ross$balance()
ross$withdraw(500)

What is the most of my interest about this code, learning the use of "$" dollar sign which refer to an specific internal function in open.account() function. I mean this part :

    ross$withdraw(30)
    ross$balance()
    robert$balance()

    ross$deposit(50)
    ross$balance()
    ross$withdraw(500)

Questions:

1- What is the meaning of the dollar sign "$" in R function() ?
2- How to identify its attributes in functions, specially for the functions that you adopting from other (i.e. you did not write it)?
I used the following script

> grep("$", open.account())
[1] 1 2 3

but it is not useful I want to find a way to extract the name(s) of internal functions that can be refer by "$" without just by calling and searching the written code as > open.account() .
For instance in case of open.account() I'd like to see something like this:

$deposit
$withdraw
$balance

3- Is there any reference that I can read more about it?
tnx!

2
See the help file: ?"$".lmo
wanted to add $ is special in that it does partial matchesArthur Yip

2 Answers

60
votes

The $ allows you extract elements by name from a named list. For example

x <- list(a=1, b=2, c=3)
x$b
# [1] 2

You can find the names of a list using names()

names(x)
# [1] "a" "b" "c"

This is a basic extraction operator. You can view the corresponding help page by typing ?Extract in R.

38
votes

There are four forms of the extract operator in R: [, [[, $, and @. The fourth form is also known as the slot operator, and is used to extract content from objects built with the S4 object system, also known as a formally defined object in R. Most beginning R users don't work with formally defined objects, so we won't discuss the slot operator here.

The first form, [, can be used to extract content from vectors, lists, or data frames.

The second and third forms, [[ and $, extract content from a single object.

The $ operator uses a name to perform the extraction as in anObject$aName. Therefore it enables one to extract items from a list based on their names. Since a data.frame() is also a list(), it's particularly well suited for accessing columns in a data frame. That said, this form does not work with a computed index, or variable substitution in a function.

Similarly, one can use the [ or [[ forms to extract a named item from an object, such as anObject["namedItem"] or anObject[["namedItem"]].

For more details and examples using each of the forms of the operator, please read my article Forms of the Extract Operator.

Accessing names in an S3 object

Daniel's post includes code for an R object, open.account(). As specified, this object is based on the S3 object system, where the behaviors of an object are defined as items within a list().

The code creates three functions within the list(), deposit, withdraw, and balance. Since each function is assigned a name, the functions within open.account() can be listed with the names() function, as illustrated below.

> names(open.account())
[1] "deposit"  "withdraw" "balance" 
>