Day 17 - Functions

Fall 2022

Dr. Jared Joseph

October 17, 2022

Overview

Timeline

  • Function Fundamentals
  • Function Creation

Goal

Delve deeper into functions and how they work.

Function Fundamentals

Function Examples

We’ve used a few functions so far:

  • mean()
  • sum()
  • table()
  • read.csv()
  • skim()
  • ifelse()
  • ggplot()
  • pivot_wider()

Anatomy of a Function





object <- function(arguments)

Anatomy of Function Creation

function_name <- function(arguments) {

    Step 1

    Step 2

    Step 3

    Etc…

}

ifelse

```{r}
ifelse
```
function (test, yes, no) 
{
    if (is.atomic(test)) {
        if (typeof(test) != "logical") 
            storage.mode(test) <- "logical"
        if (length(test) == 1 && is.null(attributes(test))) {
            if (is.na(test)) 
                return(NA)
            else if (test) {
                if (length(yes) == 1) {
                  yat <- attributes(yes)
                  if (is.null(yat) || (is.function(yes) && identical(names(yat), 
                    "srcref"))) 
                    return(yes)
                }
            }
            else if (length(no) == 1) {
                nat <- attributes(no)
                if (is.null(nat) || (is.function(no) && identical(names(nat), 
                  "srcref"))) 
                  return(no)
            }
        }
    }
    else test <- if (isS4(test)) 
        methods::as(test, "logical")
    else as.logical(test)
    ans <- test
    len <- length(ans)
    ypos <- which(test)
    npos <- which(!test)
    if (length(ypos) > 0L) 
        ans[ypos] <- rep(yes, length.out = len)[ypos]
    if (length(npos) > 0L) 
        ans[npos] <- rep(no, length.out = len)[npos]
    ans
}
<bytecode: 0x000001a6d6aa25a8>
<environment: namespace:base>

Function Creation

Anatomy of Function Creation

function_name <- function(arguments) {

    Step 1

    Step 2

    Step 3

    Etc…

}

Universe in a Box

You should treat the inside of a function like a mini R universe.


You should only work on data passed to the inside of a function through the arguments.


You can call on objects in your global environment, but this is a very bad idea.

arguments

function(arguments) {

}

Our First Function

```{r}
#| output-location: column-fragment

say_me <- function(character_to_say) {
  
  # Step 1
  print("You told me to say:")
  
  # Step 2
  print(character_to_say)
  
  # Step 3
  print("I did it!")
}

say_me(character_to_say = "Hello World!")
```
[1] "You told me to say:"
[1] "Hello World!"
[1] "I did it!"

Functions with Outputs

```{r}
#| output-location: column-fragment

add_me <- function(x, y) {
  
  # Step 1
  result = x + y
  
  # Return Results
  return(result)
}

result = add_me(1, 5)

result
```
[1] 6

Argument Defaults

```{r}
#| output-location: column-fragment

increase_me <- function(x, y = 1) {
  
  # Step 1
  result = x + y
  
  # Return Results
  return(result)
}

# Using default
print("Using default")
increase_me(1)
increase_me(6)

# Overwrite default
print("Overwrite default")
increase_me(2)
increase_me(2, 10)
```
[1] "Using default"
[1] 2
[1] 7
[1] "Overwrite default"
[1] 3
[1] 12

Help Files

Creating Thinking Machines

We are starting to create code that will work without our direct involvement.


While what we create now is simple, that will not always be the case.


The same errors we encounter now plague much more serious endeavors.

Code Along

For Next Time

Topic

R Debugging Tools and Conditions