From huber at ebi.ac.uk  Mon Feb 14 11:12:36 2005
From: huber at ebi.ac.uk (Wolfgang Huber)
Date: Mon Feb 14 12:13:24 2005
Subject: [Rd] sprintf - was formatC with illegal input crashes Rgui
	(PR#7686)
In-Reply-To: <Pine.LNX.4.61.0502141052360.30303@gannet.stats>
References: <20050211175824.5EB48DEE7@slim.kubism.ku.dk>
	<Pine.LNX.4.61.0502140956020.29523@gannet.stats>
	<4210741A.8040606@ebi.ac.uk>
	<Pine.LNX.4.61.0502141052360.30303@gannet.stats>
Message-ID: <42107994.9070500@ebi.ac.uk>

Prof Brian Ripley wrote:
> On Mon, 14 Feb 2005, Wolfgang Huber wrote:
> 
>> Dear Prof. Ripley,
>>
>> Would it be possible to make sprintf accept vector arguments?
>> I.e. allow expressions like
>>  sprintf("%04d", 1:3)
>> to produce something like what currently needs to be done via
>>  sapply(1:3, function(i) sprintf("%04d",i))
> 
> 
> I have thought about this, but it is not really clear what the right 
> thing would be here with multiple arguments.  What should
> 
> sprintf(("%04d %s", 1:3, "abc")
> 
> do?  Produce a character vector of length 1 or 3?  If of length 1,
> "0001 0002 0003 abc" or "0001 2 0003 abc"?
> 
> I guess the most R-like thing would be to recycle args to the length of 
> the longest and then use them in parallel, but that can be done fairly 
> easily by *apply.

Personally, I would prefer the recycling, but of course it can be also 
done this way:

 > mapply(sprintf, "%04d %s", 1:3, "abc")
    %04d %s       <NA>       <NA>
"0001 abc" "0002 abc" "0003 abc"

the only slightly unaesthetic thing being the names of the resulting vector.

-- 
Best regards
   Wolfgang

-------------------------------------
Wolfgang Huber
European Bioinformatics Institute
European Molecular Biology Laboratory
Cambridge CB10 1SD
England
Phone: +44 1223 494642
Fax:   +44 1223 494486
Http:  www.ebi.ac.uk/huber


[Rd] sprintf - was formatC with illegal input crashes Rgui (PR#7686)
Wolfgang Huber