- When an error occurs, the first thing that I usually do is look at the stack trace by calling traceback(): that shows you where the error occurred, which is especially useful if you have several nested functions.
- Next I will set options(error=recover); this immediately switches into browser mode where the error occurs, so you can browse the workspace from there.
- If I still don't have enough information, I usually use the debug() function and step through
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up some example data | |
year <- sample(1970:2008, 1e6, rep=T) | |
state <- sample(1:50, 1e6, rep=T) | |
group1 <- sample(1:6, 1e6, rep=T) | |
group2 <- sample(1:3, 1e6, rep=T) | |
myFact <- rnorm(100, 15, 1e6) | |
weights <- rnorm(1e6) | |
myDF <- data.frame(year, state, group1, group2, myFact, weights) | |
system.time({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pb <- txtProgressBar(style = 3, min = 0, max = length(files_list)) | |
i <- 0 | |
for (file in files_list){ | |
i <- i+1 | |
setTxtProgressBar(pb, i) | |
do_computations() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# downloads a gist | |
# usage: download_gist("4539629", "4539629.R") | |
# | |
# see also: source_gist {devtools} | |
download_gist <- function (entry, destfile, ...) | |
{ | |
if (is.numeric(entry) || grepl("^[[:digit:]]+$", entry)) { | |
entry <- paste("https://raw.github.com/gist/", entry, | |
sep = "") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nrow <- 100000 | |
ncol <- 100 | |
big_matrix <- matrix(sample(c(0,1,2), nrow * ncol, replace = TRUE), ncol = ncol) | |
print(object.size(big_matrix), units = "Mb") | |
# 76.3 Mb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# generate example: | |
prob <- c(2, rep(1,9)) | |
v <- vector() | |
for (i in 1:10000){ | |
v <- c(v, sample(1:10, 1, prob = prob)) | |
} | |
# print the output: | |
print(table(v)) |
NewerOlder