splusers -
it seems that the problem i was having was another one of those 'going
from one software system to another issues'. although i'd
set up the variable name correctly, i did not realize that it
would need to be explicitly pasted to the file to work properly
e.g. cumareas.file <- get(paste("cumareaq", filename, sep="."), immediate=T)
once done, the program works great (and now i'll have to spend less
time editing each program to run on each basin!)!
thanks to: N. Locantore, D. Bates,Z. T. Taylor, and J. Adams for
their assistance...again, i find that the sum of us is so much greater
than the parts!
my original question is listed first, the replies are listed after:
> i have several matrices of data, all of them need specific functions
> run on them.
> each matrix of data is named lsq.<bsn> where the <bsn> is the variable
> name
> of each basin, e.g. lsq.che is the matrix of data from the chehalis basin
>
> what i want to do is to write a function that does the needed processing
> where all i have to do is feed the function the suffix (i.e. <bsn>)
> so that from the command line i would type: myfunction(che)
> and the function would run on the chehalis dataset. at present, i've
> just been editing the function and changing the name of the <bsn> value,
> but surely there is an elegant way to do this!
>
> so, this is my meager attempt at doing this using a plotting function:
> in this example, i'm trying to use the 'file' as the variable name
> but i get 1) no plot and 2) error message saying that cumareas.file is not
> found
>
> function(file, filename = deparse(substitute(file)))
> {
>plot(cumareas.file, (lsslp.file[, 4] * 1.5), ylim = c(0, 150), type = "b",
> lty = 1, lwd = 1.5, xlab = "Cum.Area", ylab ="Freq.Pct", pch = "s")
>par(new = T, xaxs = "d", yaxs = "d")
>plot(cumareaq.file, (lsq.file[, 4] * 1.5), xlab = " ", ylab = " ",type = "b",
> lty = 2, lwd = 1.5, pch = "q")
>text(100000, 100, "file")
> }
>
> thank you in advance for your help
> lmv
------------------------
jean_adams@usgs.gov
Using eval(parse(text=)) might do the trick for you. Here's a quick
example:
a.big <- 1:10
b.big <- 2:11
a.bob <- 3:12
b.bob <- 4:13
tryit <- function(basin) {
a <- eval(parse(text=paste("a", basin, sep=".")))
b <- eval(parse(text=paste("b", basin, sep=".")))
a+b
}
tryit("big")
tryit("bob")
Your function might look like this:
function(basin) {
lsslp <- eval(parse(text=paste("lsslp", basin, sep=".")))
lsq <- eval(parse(text=paste("lsq", basin, sep=".")))
cumareas <- eval(parse(text=paste("cumareas", basin, sep=".")))
cumareaq <- eval(parse(text=paste("cumareaq", basin, sep=".")))
plot(cumareas, (lsslp[, 4] * 1.5), ylim=c(0, 150), type="b",
lty=1, lwd=1.5, xlab="Cum.Area", ylab="Freq.Pct", pch="s")
par(new=T, xaxs="d", yaxs="d")
plot(cumareaq, (lsq[, 4] * 1.5), xlab=" ", ylab=" ",
type="b", lty=2, lwd=1.5, pch="q")
text(100000, 100, basin)
}
From: Todd.Taylor@pnl.gov (Z. Todd Taylor)
You're part-way there. You correctly extracted the text name
of the input file [filename = deparse(substitute(file))] but
never used it. I think you're looking for this kind of incantation:
cumareas.file <- get(paste("cumareaq", filename, sep="."), immediate=T)
lsslp.file <- get(paste("lsslp", filename, sep="."), immediate=T)
etc...
plot(...
From: Douglas Bates <bates@stat.wisc.edu>
The way to dynamically create the name of an object within a function
is to use "paste" and "get".
You could start your function with
function( basin )
{
data.mat <- get( paste( "lsq.", basin, sep = "") )
From: Nick Locantore <nick@Waratah.com>
Laura, It was unclear from your email whether the data were already in
S-PLUS or not. If it is already in S-PLUS, then the flavor of what you want
should be something like this.....
## example data
a.one <- matrix(c(1,2,3,4,5,6), nrow=3)
a.two <- matrix(c(3,3,3,4,4,4), nrow=3)
## function to use
trythis.funx <- function(inname)
{
str <- paste("a.", inname, sep='')
data <- eval(parse(text=str))
plot(data[,1], data[,2])
invisible(NULL)
}
After creating the function, you would then just type at the command prompt,
trythis.funx("one")
and the plot of the data would come up.
It will take some further tweaking on your part to get the plots to do what
you wish.
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|