[R] Functions in lists or arrays?
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Tue Apr 21 08:10:14 CEST 2009
> t <- list()
> t[[1]] <- function(b) b*2 ### NOTE, [[ not [
> t
[[1]]
function (b)
b * 2
>
Bill Venables
http://www.cmis.csiro.au/bill.venables/
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Toby
Sent: Tuesday, 21 April 2009 3:51 PM
To: r-help at r-project.org
Subject: [R] Functions in lists or arrays?
I have a problem where I need to have a "driver" style R program that will
extend itself, given more
'source("extra.R")' style lines. IE: easy to modify by other people. The
problem becomes when I
would like to create an array or list of functions. Each function, possibly
hundreds of them, are really
created by other programs, generating an *.R file. So for example, if I
try:
> t <- list()
> t[1] <- function(b) { b*2 }
Error in t[1] <- function(b) { :
cannot coerce type 'closure' to vector of type 'list'
Similar errors for arrays, and anything else I can think of. I'm an R
neophite, which likely shows. The
only way I seem to be able to do the above, is to generate unique names for
each function, and add
each name into a list, sort of like this:
# Register ourselves
models <- cbind(models, "Nasa_PART_Rules")
bounds <- cbind(bounds, "Nasa_PART_Bounds")
Nasa_PART_Rules <- NULL
Nasa_PART_Bounds <- NULL
# Rules section
Nasa_PART_Rules <- rbind(Nasa_PART_Rules, c("Nasa_PART_R1", "F"))
Nasa_PART_R1 <- function(f) {
f[,"CYCLOMATIC_COMPLEXITY"] > 8 &
f[,"CYCLOMATIC_COMPLEXITY"] <= 60 &
f[,"LOC_TOTAL"] > 73
}
Nasa_PART_Bounds <- rbind(Nasa_PART_Bounds, c("Nasa_PART_B1"))
Nasa_PART_B1 <- function(b) {
b["CYCLOMATIC_COMPLEXITY",0] <- 8
b["CYCLOMATIC_COMPLEXITY",1] <- 60
b["LOC_TOTAL",0] <- 73
}
#...
And then using something like this function:
# Dispatch a function from its name
dispatch <- function(f, x) {
eval(call(f, x))
}
to evaluate each rule over all the data rows:
# Read training+validation data
dat <- read.csv("jm1_data(training+validation).csv")
mat <- NULL
clt <- NULL
# Evaluate each rule against the dataset
for (i in models) {
# Get the rules for the model
rules <- eval(substitute(foo[,1], list(foo=as.name(i))))
cls <- eval(substitute(foo[,2], list(foo=as.name(i))))
res <- lapply(rules, dispatch, dat)
#...
Now, this seems way too uggly to me. Can someone give me a hand and/or
point me into a more sane
direction to explore?
One option I have thought of, is to get rid of the *_B?() functions and just
fill in a 3 dimensional array using
something like:
x <- NULL
dimnames(x) <- c(colnames(mat),colnames(dat), c("lbound","ubound"))
...
x["RULE_NAME_1", "DATA_COL_NAME_1", "lbound"] <- ...
...
But I'm not exactly sure how I would construct and/or add onto a global
array/etc extra dimnames, as I source
each generated *.R file.
Anyways, Not sure if I'm making much sense... thanks for any help,
-Toby.
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list