Skip to content

Commit

Permalink
Merge pull request #16 from nickgood/error_checking
Browse files Browse the repository at this point in the history
Error checking
  • Loading branch information
nickgood authored Sep 27, 2019
2 parents e675ea7 + 2db4694 commit 870b6f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
36 changes: 36 additions & 0 deletions slides/CourseNotes_Week12.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ library(knitr)
library(ggmap)
```

# Functions-- Error checking

## Functions-- Error checking

There are ways to check for errors in the arguments a user inputs to the function. One useful check is to see if user inputs are in the required class.

The `assertive` package has some functions that you can use for common checks of the inputs to a function. If someone inputs something of the wrong class, it will give a useful error message. For example, the `assert_is_numeric` function will check if an object is in a numeric class. If so, it will do nothing. If not, it will return an error message:

```{r error = TRUE}
library(assertive)
assert_is_numeric(1:3)
assert_is_numeric(c("a", "b"))
```

## Functions-- Error checking

You could add this in the code for the `add_one` function, so a useful error message will be returned if a user tries to input something besides a numeric vector for `number`:

```{r}
add_one <- function(number){
assert_is_numeric(number)
number + 1
}
```

```{r error = TRUE}
add_one(number = 1:3)
add_one(number = c("a", "b"))
```

## Functions-- Error checking

I would recommend that you not worry about this too much when you're learning to write functions for your own use.

However, once you have mastered the basics of writing functions and start writing them for others to use, you'll want to start incorporating this.

# Shiny applications

## Resources for learning Shiny
Expand Down
34 changes: 0 additions & 34 deletions slides/CourseNotes_Week8a.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -196,40 +196,6 @@ add_one <- function(number = 0){

If using `return` helps you think about what's happening with the code in your function, you can use it. However, outside of a few exceptions, you usually won't need to do it.

## Functions-- Error checking

There are ways to check for errors in the arguments a user inputs to the function. One useful check is to see if user inputs are in the required class.

The `assertive` package has some functions that you can use for common checks of the inputs to a function. If someone inputs something of the wrong class, it will give a useful error message. For example, the `assert_is_numeric` function will check if an object is in a numeric class. If so, it will do nothing. If not, it will return an error message:

```{r error = TRUE}
library(assertive)
assert_is_numeric(1:3)
assert_is_numeric(c("a", "b"))
```

## Functions-- Error checking

You could add this in the code for the `add_one` function, so a useful error message will be returned if a user tries to input something besides a numeric vector for `number`:

```{r}
add_one <- function(number){
assert_is_numeric(number)
number + 1
}
```

```{r error = TRUE}
add_one(number = 1:3)
add_one(number = c("a", "b"))
```

## Functions-- Error checking

I would recommend that you not worry about this too much when you're learning to write functions for your own use.

However, once you have mastered the basics of writing functions and start writing them for others to use, you'll want to start incorporating this.

## `if` / `else`

In R, the `if` statement evaluates everything in the parentheses and, if that evaluates to `TRUE`, runs everything in the braces. This means that you can trigger code in an `if` statement with a single-value logical vector:
Expand Down

0 comments on commit 870b6f8

Please sign in to comment.