The Art of R Programming

(WallPaper) #1

  1. We told GDB to resume executing R (we needed to hit theENTERkey a
    second time in order to get the R prompt):
    (gdb) continue


We then executed our C code:


m <- rbind(1:5, 6:10, 11:15, 16:20, 21:25)
k<-2
.C("subdiag", as.double(m), as.integer(dim(m)[1]), as.integer(k),



  • result=double(dim(m)[1]-k))



Breakpoint 1, subdiag (m=0x942f270, n=0x96c3328, k=0x96c3348, result=0x9a58148)
at subdiag.c:46
46 if (*n < 1) error("n < 1\n");


At this point, we can use GDB to debug as usual. If you’re not familiar
with GDB, you may want to try one of the many quick tutorials on the Web.
Table 15-1 lists some of the most useful commands.

Table 15-1:Common GDB Commands

Command Description
l List code lines
b Set breakpoint
r Run/rerun
n Step to next statement
s Step into function call
p Print variable or expression
c Continue
h Help
q Quit

15.1.5 Extended Example: Prediction of Discrete-Valued Time Series.......


Recall our example in Section 2.5.2 where we observed 0- and 1-valued data,
one per time period, and attempted to predict the value in any period from
the previouskvalues, using majority rule. We developed two competing
functions for the job,preda()andpredb(), as follows:

# prediction in discrete time series; 0s and 1s; use k consecutive
# observations to predict the next, using majority rule; calculate the
# error rate
preda <- function(x,k) {
n <- length(x)
k2 <- k/2
# the vector pred will contain our predicted values
pred <- vector(length=n-k)

Interfacing R to Other Languages 327
Free download pdf