[R] Working with and without formula in function

2009-04-03 Thread dogle

I am attempting to write a function that is flexible enough to respond to the
user providing a formula (with a data= argument) or not (similar to
plot(x,y) versus plot(y~x,data=data)).  I have found a method to work with
this in a simple case but am having trouble determining how to "find" a
variable from within the data= argument that is not part of the formula. 
The following illustrates my problem ...

# an illustrative function -- I know that plotrix::thigmophobe.labels() does
what this function does
myplot <- function(x,y=NULL,data=NULL,label=NULL) {
  if (class(x)=="formula") {
mf <- model.frame(x,data=data)
x <- mf[,2]
y <- mf[,1]
  } 
  if (is.null(y)) stop("Y-axis variable is missing")
  plot(x,y)
  if (!is.null(label)) text(x,y,label)
}

# dummy data
df <-
data.frame(x=runif(10),y=runif(10),grp=factor(rep(c("Yes","No"),each=5)))

# both calls work as expected
with(df,myplot(x,y))
myplot(y~x,data=df)

# only first works as I would hope
with(df,myplot(x,y,label=grp))
myplot(y~x,data=df,label=grp)

# this works but is clumsy
myplot(y~x,data=df,label=df$grp)


Any help with how to make this function recognize the "grp" variable in "df"
without having to type "df$grp" when supplying it to the "label=" argument
would be greatly appreciated.  Thank you in advance.
-- 
View this message in context: 
http://www.nabble.com/Working-with-and-without-formula-in-function-tp22874005p22874005.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] Simple R (in Sweave) Question

2008-11-02 Thread dogle

I am writing a report using Sweave with specific R output incorporated into
the text using the Sexpr{} command.  I have run into two specific issues:

1)  If the result inside the Sexpr{} command is an integer less than 10 I
would like to print the “word” for that number – e.g., “seven” for 7.
2)  If a p-value is “equal to zero” to some arbitrary number of decimal
places -- e.g., formatC(pvalue,format=”f”,digits=4) returns 0. -- I
would like to print “<0.5”.

I have attempted searching R-help et al but am not sure what keywords to use
for these questions.  I can picture the format of functions to solve these
issues but would be interested in pointers to previous solutions if they
exist.

Thank you in advance for any help.

> sessionInfo()
R version 2.7.2 (2008-08-25) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] tcltk stats graphics  grDevices utils datasets  methods  
base 

other attached packages:
 [1] xtable_1.5-3  RODBC_1.2-3   lattice_0.17-13  
 [4] chron_2.3-24  FSA_0.0-13reshape_0.8.0
 [7] Rcapture_1.1  quantreg_4.20 SparseM_0.78 
[10] plotrix_2.4-7 MASS_7.2-44   exactRankTests_0.8-17
[13] NCStats_0.0-12TeachingDemos_2.3 sciplot_1.0-3
[16] nortest_1.0   multcomp_1.0-2mvtnorm_0.9-2
[19] Hmisc_3.4-3   gplots_2.6.0  gtools_2.5.0 
[22] car_1.2-8 FSAdata_0.0-5 gdata_2.4.2  

loaded via a namespace (and not attached):
[1] cluster_1.11.11 grid_2.7.2  tools_2.7.2


-- 
View this message in context: 
http://www.nabble.com/Simple-R-%28in-Sweave%29-Question-tp20295139p20295139.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Automatic placement of Legends

2008-07-08 Thread dogle

You may be able to use the coordinates returned from emptyspace() in plotrix
package as coordinates for the legend in legend().


tolga.i.uzuner wrote:
> 
> ...
> I am looking for a way to get legends placed automagically in an empty 
> spot on a graph. Additional complication comes through my useage of 
> multiple graphs on the same plot through mfrow. 
> 
> Is there a way to achieve this in R ? I have legends for each of the 
> sub-plots.
> ...
> 

-- 
View this message in context: 
http://www.nabble.com/Automatic-placement-of-Legends-tp18346098p18348418.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.