[R] wrapper function in R
Kruti Pandya
kp1005 at gmail.com
Mon Mar 16 00:03:22 CET 2015
I want to extract each of the logical expressions of
generateRandomNKNetwork function and evaluate each expression as follows:
h[[1]]<-function (x) {(!x[3] & x[1]) | (x[3] & !x[1])}
h[[1]](c(data[,1]))
[1] TRUE
h[[1]](c(data[,2]))
[1] TRUE
I applied a wrapper function to pass the elements of g[[i]] in h[[i]] and
make it a function of x.So that I can evaluate each of the Boolean
expression for each row of my data matrix.
My wrapper function doesn’t work. I do not get an error but when I list
elements of h it is blank. Will appreciate any help.Also, are there any
good resources to learn functional programming in R ?
install.packages("BoolNet")
library(BoolNet)
n<-generateRandomNKNetwork(3,2,readableFunctions="canonical")
# wrapper function to extract the boolean functions
wrapper <-function(y) {function(x) {(y)}}
# Create random binary matrix
gendata <- function(n,p ) {
matrix(rbinom(n * p, 1, 0.5), ncol = p, nrow = n)
}
data<-gendata(5,3)
#function to extract logical expressions
f=list()
g=list()
h=list()
for (i in 1:3){
f[[i]]<-noquote(n$interactions[[paste0("Gene",i)]]$expression)
g[[i]]<-gsub("(Gene)([[:digit:]])", "x[\\2]", f[[i]])
h[[i]]<-wrapper(g[[i]]) #apply wrapper function
#evaluate h at each row of data
#for(i in 1:nrow(data)) {
#h[[i]](c(data[i,])
#}
}
> h
[[1]]
function (x)
{
(y)
}
<environment: 0x00000000283fa8c0>
[[2]]
function (x)
{
(y)
}
<environment: 0x00000000283ec018>
[[3]]
function (x)
{
(y)
}
<environment: 0x00000000283e4b38>
[[alternative HTML version deleted]]
More information about the R-help
mailing list