[R] Loop function/comparison operator problem

David Winsemius dwinsemius at comcast.net
Tue Oct 6 02:23:51 CEST 2009


On Oct 5, 2009, at 7:56 PM, Duncan Murdoch wrote:

> On 05/10/2009 7:47 PM, jimdare wrote:
>> Hi There,
>> I have created the following function
>> format<- function(){
>> repeat {
>> form<-readline(paste("\nIn what format do you want to save these
>> plots?\nChoose from: wmf, emf, png, jpg, jpeg, bmp, tif, tiff, ps,  
>> eps, or
>> pdf.\nNote: eps is the suggested format for publication quality  
>> plots.\nPlot
>> format --> "));
>> 	cat("\nI'm sorry, I don't know what that format is.\nPlease try
>> again\nPress ENTER...");readline()}
>> 	if (form == c("wmf", "emf", "png", "jpg", "jpeg", "bmp", "tif",  
>> "tiff",
>> "ps", "eps", "pdf")) {break}
>> }
>> How do I get the program to recognise that the user has entered one  
>> of the
>> formats in the last line?  Even entering "png" insted of png at the  
>> prompt
>> doesn't seem to work.  Will the loop only break if I enter all of the
>> possible formats?
>
> To debug stuff like this, assign a value to form, then evaluate the  
> condition.  For example,
>
> > form <- "png"
> > form == c("wmf", "emf", "png", "jpg", "jpeg", "bmp", "tif", "tiff",
> +  "ps", "eps", "pdf")
> [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> So you could put any() around the test, or use the %in% operator  
> instead of ==, or use the menu() or select.list() functions.

The ?Devices help page suggests that there is a core of valid formats  
and a group of installation specific formats. the capabilities()  
functions gives logical values for the list of both graphics and non- 
graphics optional functions. This is a hack to return a list of core  
features with additional system specific options (illustrated on a my  
particular Mac):

 > val.dev <- c("ps" ,"pdf", "pictex", "xfig", "bitmap",  
names(capabilities()[names(capabilities()) %in% c("X11", "bmp",  
"jpeg", "png", "tiff", "cairo_pdf", "quartz")]) )
 > val.dev
[1] "ps"     "pdf"    "pictex" "xfig"   "bitmap" "jpeg"   "png"     
"tiff"   "X11"

The fact that the valid types are in the names makes it all a bit  
Baroque. (And I think that a correct Mac version would furhter check  
to see if aqua is true and if so return "jpeg", "png" and "tiff" even  
if those were not listed as part of the X11 capabilities.)

>
> Duncan Murdoch
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list