dat !<- data.frame(x = 1, y = 2) df$x #> Error in df$x: object of type 'closure' is not subsettable cat("Perhaps you wanted `dat$x`?\n") #> Perhaps you wanted `dat$x`?
WHAT'S YOUR MAIN DEBUGGING METHOD? A. I don’t have a method. Why do you think I’m here?!? B. I ask in an online forum or consult my local expert. C. Google is my BFF. D. Print debugging is my jam. E. I use R's debugging tools, like browser(). Poll at slido.com (event code = HEXAGON)
WHICH PERSIST AFTER rm(list = ls())? A. library(dplyr) B. summary <- head C. options(stringsAsFactors = FALSE) D. Sys.setenv(LANGUAGE = "fr") E. x <- 1:5 F. attach(iris) Poll at slido.com (event code = HEXAGON)
WHICH PERSIST AFTER rm(list = ls())? A. library(dplyr) ✔ B. summary <- head ✘ C. options(stringsAsFactors = FALSE) ✔ D. Sys.setenv(LANGUAGE = "fr") ✔ E. x <- 1:5 ✘ F. attach(iris) ✔ Poll at slido.com (event code = HEXAGON)
tidyverse/dplyr#4094 inlined data 2 lines of code 1 package 1 function dat !<- data.frame(`!..1` = 1) dplyr!::mutate_all(dat, as.character) #> Error: Column 1 must not have names #> of the form !!... or !..j. #> Use .name_repair to specify repair.
HAVE YOU GOTTEN STUCK IN THE DEBUGGER? A. R has a debugger? What is a debugger? B. No. C. Yes. D. Yes and I'm not even sure how I got there. Poll at slido.com (event code = HEXAGON)
dat #> blackberry blueberry peach plum #> calories 4 1 59 30 #> weight 9 2 150 78 #> yumminess 6 8 10 5 fruit_avg(dat, pattern = "black") #> Found fruits! #> Error in rowMeans(mini_dat): 'x' must be an array #> of at least two dimensions
dat #> blackberry blueberry peach plum #> calories 4 1 59 30 #> weight 9 2 150 78 #> yumminess 6 8 10 5 fruit_avg(dat, pattern = "black") #> Found fruits! #> Error in rowMeans(mini_dat): 'x' must be an array #> of at least two dimensions
base fruit_avg(dat, pattern = "black") #> Found fruits! #> Error in rowMeans(mini_dat): 'x' must be an array #> of at least two dimensions traceback() #> 3: stop("'x' must be an array of at least two dimensions") #> 2: rowMeans(mini_dat) at fruit_avg.R#5 #> 1: fruit_avg(dat, pattern = "black")
rlang fruit_avg(dat, pattern = "black") #> Found fruits! #> Error in rowMeans(mini_dat): 'x' must be an array #> of at least two dimensions rlang!::last_trace() #> #> 'x' must be an array of at least two dimensions #> Backtrace: #> █ #> 1. └─global!::fruit_avg(dat, pattern = "black") #> 2. └─base!::rowMeans(mini_dat) R/fruit_avg.R:5:2
options(error = recover) fruit_avg(dat, "black") #> Found fruits! #> Error in rowMeans(mini_dat): 'x' must be an array #> of at least two dimensions Enter a frame number, or 0 to exit 1: fruit_avg(dat, "black") 2: fruit_avg.R#5: rowMeans(mini_dat) Selection: 1
ADD AN ASSERTION dat !<- read.csv("fruit.csv") if (!all(vapply(dat, is.numeric, logical(1)))) { stop("All columns of `dat` must be numeric") } fruit_avg(dat, pattern = "berry")
The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at and repair. Douglas Adams
WRITE ERROR MESSAGES FOR HUMANS dat !<- data.frame(x = 1, y = 2) df$x #> Error: Can't subset a function. #> Have you forgotten to define a variable named `df`?
library(dplyr) filter(iris, Species = "setosa") #> Error: `Species` (`Species = "setosa"`) must not be #> named, do you need `!==`? WRITE ERROR MESSAGES FOR HUMANS
@jennybc @JennyBryan Jennifer Bryan RStudio rstd.io/debugging Turn it off and on again Make a reprex Dig into the error Plan for the unexpected Thanks: Tidyverse team Christine Kuper