s-news
[Top] [All Lists]

Re: Code that works outside a custom function but not inside

To: "Rongsheng Liu" <rsliu@homer.att.com>
Subject: Re: Code that works outside a custom function but not inside
From: "s b" <sbmailinglist@gmail.com>
Date: Thu, 30 Oct 2008 12:53:36 -0500
Cc: s-news@lists.biostat.wustl.edu
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type:references; bh=Pq6V60YWyJ6PpUfTbIYwU6eprqiOr0JzmT75NzHYYAo=; b=rrnlG1/eFrAYXfJcLeIWS7+msEa0gE9HURc11lE1SXB/1E7NVgjWAJscpYf41cVZC7 JGrcWw8S5hCH0B6ELQHJ2NMZmAt/sV52/d6WcXaYKAXR472SUyzej5SYlG4gFnJrpGM8 U24IBWKBtfpCV/S2wIgpyA0dtHBND/UvNQ2Lw=
Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:references; b=l0QESNibf6OKe8PKR7WI5fFllaZmpxX1SQe1XfUM5O6/m0ks3GEgAZqbk9MNBVh+IZ QL9jeOMMb4d1SpztxKP+JcKMqg1mpHWi+6gcOcLTZHC3k/VxL19zqL4dA1ejgG/uAxTJ AAOgFD1g1YReeuIYgN0n+v3/3brARlfTWfmVk=
In-reply-to: <MHEMKBJBACHOJNHMBNPOIECCCFAA.rsliu@homer.att.com>
References: <29ee5dab0810300719p60c08034t97ea2ca1bfe01ba7@mail.gmail.com> <MHEMKBJBACHOJNHMBNPOIECCCFAA.rsliu@homer.att.com>
Hi Ron,

I am really a programmer but I am switching form R or S-plus. So, there are still some tricks I do not know. Actually, I found the source of my problem: the formula should use the name of the column in the data.frame and not custom vector like mydata[,1].

Any idea about how to customize the text displayed in the strip?

On Thu, Oct 30, 2008 at 11:01 AM, Rongsheng Liu <rsliu@homer.att.com> wrote:
Sebastien,
 
The easiest way is:
        #put a line at the beginning of the function:
        assign("mydata", mydata, frame=1)
 
May be it is not a good way for a programmer, but it works.
 
Thanks,
Ron 
 
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of s b
Sent: Thursday, October 30, 2008 9:20 AM
To: Rongsheng Liu
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] Code that works outside a custom function but not inside

Hi Ron,

Thanks for your input. I have however tested my code with and without the strip function and still had the same error message:

Problem in eval(if(no.given) formula else expr[[1]],..: Object "mydata" not found

Sebastien

On Thu, Oct 30, 2008 at 10:10 AM, Rongsheng Liu <rsliu@homer.att.com> wrote:
Sebastien,
 
try to passing parameter into strip function:
           
          strip = function(which.panel, nID=nID, ...){
                        lines(c(0,1,1,0,0),c(0,0,1,1,0),col=1)
                        text(rep(0.5,nID),rep(0.5,nID),
                        paste("SD = ",mysds[which.panel],sep=""),
                        cex = 0.75)}
Thanks,
Ron

 

 

 

-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of s b
Sent: Thursday, October 30, 2008 8:21 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] Code that works outside a custom function but not inside

Dear S-plus users,

I am trying to plot some density histograms from a data.frame of simulated data. Since I want to plot several sets of simulated data, I have created this function myhisto which supposedly should do the work. Funny thing: the code within the function does not work when the data are passed to the function, BUT it does when it is used outside of a function (see the end of the code below). Can anyone explain what I am doing wrong in my function?

Thanks you in advance,
Sebastien

PS: if anyone can also explain why the attribute which.panel does not work in strip, I would also be grateful!


############# Code starts here

# Custom functions
# Random generation of 10000 individuals for each input sd

myranddf <- function(mysds){
  nsd <- length(mysds)
  tmp <- c()
  for (i in seq(nsd)){
    tmp <- c(tmp,rnorm(10000, mean = 0, sd = mysds[i]))
  }
  mydf <- data.frame(X=tmp, SD=rep(mysds, each = 10000))
  mydf
}


# Histogram plot

myhisto <- function(mydata,nBar){

  mysds <- levels(factor(mydata[,2]))
  nID <- length(mysds)
 
  myplot <- histogram(~mydata[,1]|mydata[,2],
                      data = "">                      as.table=TRUE,
                      type = "percent",
                      nint=nBar,
                      xlim=c(0,1),
                      xlab="X",
                      ylab="Probability density",
                      layout=c(2,2),
                      main=NULL,
                      strip = function(which.panel,...){
                        lines(c(0,1,1,0,0),c(0,0,1,1,0),col=1)
                        text(rep(0.5,nID),rep(0.5,nID),
                             paste("SD = ",mysds[which.panel],sep=""),
                             cex = 0.75)},
                      key=NULL)
  print(myplot)
}

# Create data set for typical logit transform with additive variability model
# X = exp(ln(TVX/(100-TVX))+eta)/(1+exp(ln(TVX/(100-TVX))+eta))

TVX = 50
mysds = c(0.05,0.1,0.25,0.75)*TVX

mydf <- myranddf(mysds)
mydf[,1] <- exp(log(TVX/(100-TVX))+mydf[,1])
mydf[,1] <- mydf[,1]/(1+mydf[,1])

# Plotting using the function

myhisto(mydf,50)

# Plotting outside a function

mydata <- mydf
nBar <- 50

  myplot <- histogram(~mydata[,1]|mydata[,2],
                      data = "">                      as.table=TRUE,
                      type = "percent",
                      nint=nBar,
                      xlim=c(0,1),
                      xlab="X",
                      ylab="Probability density",
                      layout=c(2,2),
                      main=NULL,
                      strip = function(which.panel,...){
                        lines(c(0,1,1,0,0),c(0,0,1,1,0),col=1)
                        text(rep(0.5,4),rep(0.5,4),
                             paste("SD = ",mysds[which.panel],sep=""),
                             cex = 0.75)},
                      key=NULL)
  print(myplot)

################ END of code


<Prev in Thread] Current Thread [Next in Thread>