## Paul Johnson <pauljohn@ku.edu>
## 2012-01-30
## Special thanks to r-help email list contributors,
## especially Henrik Bengtsson. Observe meaning of "parse"
## and eval. parse creates an expression, from the "text"
## we type in. eval convertst that to results.
x1 <- "rgamma(10, shape = 3, scale = 2)"
class(x1)
## [1] "character"
eval(x1)
## [1] "rgamma(10, shape = 3, scale = 2)"
x1.parsed <- parse(text = x1)
class(x1.parsed)
## [1] "expression"
eval(x1.parsed)
## [1] 3.977 1.795 13.321 2.818 15.881 2.977 4.446 5.706 4.424 11.829
## Get it? x1 is a character string we type in.
## parse converts that to a format that R understands.
## Now, embed a text character variable inside a matrix
## that is filled with character strings..
BM <- matrix("0.1", 5, 5)
BM[2,1] <- "a"
BM[3,2] <- "b"
BM
## [,1] [,2] [,3] [,4] [,5]
## [1,] "0.1" "0.1" "0.1" "0.1" "0.1"
## [2,] "a" "0.1" "0.1" "0.1" "0.1"
## [3,] "0.1" "b" "0.1" "0.1" "0.1"
## [4,] "0.1" "0.1" "0.1" "0.1" "0.1"
## [5,] "0.1" "0.1" "0.1" "0.1" "0.1"
parseAndEval <- function(x, ...) eval(parse(text=x))
a <- 0.5
b <- 0.4
realBM <- apply(BM, MARGIN=c(1,2), FUN=parseAndEval)
BM[4,5] <- "rnorm(1, m=7, sd=1)"
BM
## [,1] [,2] [,3] [,4] [,5]
## [1,] "0.1" "0.1" "0.1" "0.1" "0.1"
## [2,] "a" "0.1" "0.1" "0.1" "0.1"
## [3,] "0.1" "b" "0.1" "0.1" "0.1"
## [4,] "0.1" "0.1" "0.1" "0.1" "rnorm(1, m=7, sd=1)"
## [5,] "0.1" "0.1" "0.1" "0.1" "0.1"
realBM <- apply(BM, MARGIN=c(1,2), FUN=parseAndEval)
realBM
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.1 0.1 0.1 0.1 0.100
## [2,] 0.5 0.1 0.1 0.1 0.100
## [3,] 0.1 0.4 0.1 0.1 0.100
## [4,] 0.1 0.1 0.1 0.1 7.244
## [5,] 0.1 0.1 0.1 0.1 0.100
## Now, what about gui interaction with that table?
## The best "nice looking" options are not practical at the moment.
## Try this instead
data.entry(BM)
## That will work on all platforms, so far as I know, without
## any special effort from us. Run that, make some changes, then
## make sure you insert new R variables to match in your environment.
## Suppose you inserted the letter z in there somewhere. Go do that.
BM[1,1] <- "z"
BM
## var1 var2 var3 var4 var5
## [1,] "z" "0.1" "0.1" "0.1" "0.1"
## [2,] "a" "0.1" "0.1" "0.1" "0.1"
## [3,] "0.1" "b" "0.1" "0.1" "0.1"
## [4,] "0.1" "0.1" "0.1" "0.1" "rnorm(1, m=7, sd=1)"
## [5,] "0.1" "0.1" "0.1" "0.1" "0.1"
## z's just a letter in the character matrix. It is not defined yet.
## set z out here
z <- rpois(1, lambda=10)
realBM <- apply(BM, MARGIN=c(1,2), FUN=parseAndEval)
realBM
## var1 var2 var3 var4 var5
## [1,] 9.0 0.1 0.1 0.1 0.100
## [2,] 0.5 0.1 0.1 0.1 0.100
## [3,] 0.1 0.4 0.1 0.1 0.100
## [4,] 0.1 0.1 0.1 0.1 5.545
## [5,] 0.1 0.1 0.1 0.1 0.100