-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.R
81 lines (58 loc) · 2.19 KB
/
external.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# External Code
## Like all developers, you're the smartest person in the world
## So YOUR code can't have an error, unlike all those
## filthy filthy libraries you have to use
## How do we debug an external library or source file?
# The source file first
# fixed <- function(len) {
# x <- numeric()
# for(i in 1:len) {
# x[i] <- i + runif(1)
# }
# return(x)
# }
source("source.R")
setBreakpoint("source.R", 5)
# /Users/protonk/dev/bocoup/training/debugcast/source.R#5:
# fixed step 3 in <environment: R_GlobalEnv>
# if we call fixed() it is now mapped to that source file
# debug(externalFunctionName)
# may work, but we can always use
# trace(externalFunctionName, tracer = browser)
# without the "at" argument (or with it)
## This is a bit laborious at times.
## When we inserted browser() we picked a line number
## Can we do this with an external package?
install.packages("truncnorm")
library(truncnorm)
# Now we're stuck!
# Libraries are (by default on Windows/OS X) installed as binary data
# functions, object and methods are lazily loaded (on every platform)
# for our purposes, this means no source map
# Canonical reference is http://cran.r-project.org/doc/manuals/R-exts.html
# if we look at the files
list.files(system.file("R", package = "truncnorm"))
# We don't see any R scripts
# R tricks us (helpfully!) if we try to display the function body
rtruncnorm
# function (n, a = -Inf, b = Inf, mean = 0, sd = 1)
# {
# if (length(n) > 1)
# n <- length(n)
# if (length(n) > 1)
# n <- length(n)
# else if (!is.numeric(n))
# stop("non-numeric argument n.")
# .Call("do_rtruncnorm", as.integer(n), a, b, mean, sd)
# }
# <environment: namespace:truncnorm>
# It's not actually displaying the source code. It's displaying the
# parsed representation of the code from a stored parse tree
# we can set R to install packages and keep the displayed source code
# but it is much easier to trace functions and not worry about
# line number conversion for externally supplied packages.
trace("rtruncnorm")
# If the function name conflicts with another package,
# you may have to specify the namespace
# we can specify the same arguments here was we would tracing our
# own functions