using browser() in r

20
Interactive Debugging with Environment Browser Leon Kim | www.betweentwotests.com

Upload: leon-hj-kim

Post on 15-Apr-2017

106 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: Using browser() in R

Interactive Debugging with Environment Browser

Leon Kim | www.betweentwotests.com

Page 2: Using browser() in R

Overview of base::browser()

I This is a brief overview of base::browser() and how to useit for debugging in R.

I browser() is default function found in R.I Useful tool to “hack into” R functions (that you or other

people wrote)

Page 3: Using browser() in R

Typical day in lab

head(iris)

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 5.1 3.5 1.4 0.2 setosa## 2 4.9 3.0 1.4 0.2 setosa## 3 4.7 3.2 1.3 0.2 setosa## 4 4.6 3.1 1.5 0.2 setosa## 5 5.0 3.6 1.4 0.2 setosa## 6 5.4 3.9 1.7 0.4 setosa

I Task: Researcher’s ruler was off by 5 inches. Let’s adjust it!

Page 4: Using browser() in R

Oops

# Loop through each column and add 5df <- irisfor(j in 1:ncol(df)){

df[,j] <- df[,j] + 5}

## Warning in Ops.factor(df[, j], 5): '+' not meaningful for factors

unlist(lapply(iris, class))

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## "numeric" "numeric" "numeric" "numeric" "factor"

I Forgot that Species column is not numeric!

Page 5: Using browser() in R

Quick and dirty solution

# Loop through each column EXCEPT the last onedf <- irisfor(j in 1:(ncol(df)-1)){

df[,j] <- df[,j] + 5}head(df)

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 10.1 8.5 6.4 5.2 setosa## 2 9.9 8.0 6.4 5.2 setosa## 3 9.7 8.2 6.3 5.2 setosa## 4 9.6 8.1 6.5 5.2 setosa## 5 10.0 8.6 6.4 5.2 setosa## 6 10.4 8.9 6.7 5.4 setosa

Page 6: Using browser() in R

We got lucky

I Not all error messages are obviousI Not all bugs come from such easy codeI Not all bugs are this easy to fix

Page 7: Using browser() in R

More likely scenariofoo_1 <- function(x) { ... }foo_2 <- function(x) { ... }# Code that produces some errorfor(j in 1:ncol(df)){

foo_1(df[,j]); foo_2(df[,j])}

foos <- function(x) {foo_1(foo_2(x))

}super_foo <- function(df) {

# Code that produces some errorfor(j in 1:ncol(df)){

foos(df[,j])}

}super_foo(iris)

Page 8: Using browser() in R

browser() to the rescue

for(j in 1:ncol(df)){browser() <--------foo_1(df[,j])foo_2(df[,j])

}

Page 9: Using browser() in R

Example

add_five <- function(x) {x + 5}df <- irisoutside_var <- "I'm outside the loop"for(j in 1:ncol(df)){

print(paste("start of the iteration:", j))inside_var <- "I'm inside the loop"browser()df[,j] <- add_five(df[,j])print(paste("end of the interation:", j))

}

Page 10: Using browser() in R

Trigger browser() mode

I Run your code

I Run expressions within browser() mode

Page 11: Using browser() in R

What can I do in browser() mode?

I Type help for list of avaiable commands

Page 12: Using browser() in R

‘c’: execute all of the function

I Executes everything left (until the next browser() call that is)

I Sanity check: has the first column of df been changed?

Page 13: Using browser() in R

‘n’: Go to the next line

I Moves to beginning of the next line

I Also executes your current line (before moving to next ine)

Page 14: Using browser() in R

‘s’: R-ception

I Go into the function call

Page 15: Using browser() in R

In RStudio

I GUI supports (for function calls)

Page 16: Using browser() in R

Access everything inside the function environment

I Access variables as the function sees it

Page 17: Using browser() in R

Effectively use browser() : #1

add_five <- function(x) {x + 5}df <- irisfor(j in 1:ncol(df)){

if(j == 5) {browser()

}df[,j] <- add_five(df[,j])

}

Page 18: Using browser() in R

Effectively use browser() : #2

library(dplyr)df <- irisdf %>%

group_by(Species) %>%mutate(avg_sepal_length =

list(Species = Species,Sepal.Length = Sepal.Length) %>%

browser())

Page 19: Using browser() in R

Look at individual group

I Pass data to browser()

Page 20: Using browser() in R

tl;dr

I browser() allows you to interactively code in any environmentscope during R execution