Debian Codegraph @

R/ROCR_aux.R

raw
1 ## ---------------------------------------------------------------------------
2 ## Dealing with argument lists, especially '...'
3 ## ---------------------------------------------------------------------------
4
5 ## return list of selected arguments, skipping those that
6 ## are not present in arglist
7 .select.args <- function( arglist, args.to.select, complement=FALSE) {
8 match.bool <- names(arglist) %in% args.to.select
9 if (complement==TRUE) match.bool <- !match.bool
10 return( arglist[ match.bool] )
11 }
12
13 ## return arguments in arglist which match prefix, with prefix removed
14 ## ASSUMPTION: prefix is separated from rest by a '.'; this is removed along
15 ## with the prefix
16 .select.prefix <- function( arglist, prefixes, complement=FALSE ) {
17 match.expr <- paste(paste('(^',prefixes,'\\.)',sep=""),collapse='|')
18 match.bool <- (1:length(arglist)) %in% grep( match.expr, names(arglist) )
19 if (complement==TRUE) match.bool <- !match.bool
20 arglist <- arglist[ match.bool]
21 names(arglist) <- sub( match.expr, '', names(arglist))
22
23 return( arglist )
24 }
25
26 .garg <- function( arglist, arg, i=1) {
27 if (is.list(arglist[[arg]])) arglist[[ arg ]][[i]]
28 else arglist[[ arg ]]
29 }
30
31 .sarg <- function( arglist, ...) {
32 ll <- list(...)
33 for (argname in names(ll) ) {
34 arglist[[ argname ]] <- ll[[ argname ]]
35 }
36 return(arglist)
37 }
38
39 .farg <- function( arglist, ...) {
40 ll <- list(...)
41 for (argname in names(ll) ) {
42 if (length(arglist[[argname]])==0)
43 arglist[[ argname ]] <- ll[[ argname ]]
44 }
45 return(arglist)
46 }
47
48 .slice.run <- function( arglist, runi=1) {
49 r <- lapply( names(arglist), function(name) .garg( arglist, name, runi))
50 names(r) <- names(arglist)
51 r
52 }
53
54 ## ---------------------------------------------------------------------------
55 ## Line segments
56 ## ---------------------------------------------------------------------------
57
58 .construct.linefunct <- function( x1, y1, x2, y2) {
59 if (x1==x2) {
60 stop("Cannot construct a function from data.")
61 }
62
63 lf <- eval(parse(text=paste("function(x) {",
64 "m <- (",y2,"-",y1,") / (",x2,"-",x1,");",
65 "c <- ",y1," - m * ",x1,";",
66 "return( m * x + c)}",sep=" ")))
67 lf
68 }
69
70 #' @importFrom stats uniroot
71 .intersection.point <- function( f, g ) {
72 ## if lines are parallel, no intersection point
73 if (f(1)-f(0) == g(1)-g(0)) {
74 return( c(Inf,Inf) )
75 }
76
77 ## otherwise, choose search interval
78 imin <- -1
79 imax <- 1
80 while (sign(f(imin)-g(imin)) == sign(f(imax)-g(imax))) {
81 imin <- 2*imin
82 imax <- 2*imax
83 }
84 h <- function(x) { f(x) - g(x) }
85
86 intersect.x <- stats::uniroot( h, interval=c(imin-1,imax+1) )$root
87 intersect.y <- f( intersect.x )
88 return( c(intersect.x, intersect.y ))
89 }