ehlers@math.ucalgary.ca
2005-Feb-11 18:58 UTC
[Rd] formatC with illegal input crashes Rgui (PR#7686)
Full_Name: Peter Ehlers Version: rw2001pat (2005-02-03) OS: Win XP Submission from: (NULL) (136.159.61.115) formatC(1, flag="s") crashes Rgui. Similarly for flag=[SnZ]. Stupid input, of course, but I'm error-prone. Peter
Prof Brian Ripley
2005-Feb-14 11:01 UTC
[Rd] formatC with illegal input crashes Rgui (PR#7686)
We'll fix this, but you might like to note that sprintf is more bulletproof and as from 2.1.0-to-be, a lot more tolerant, e.g. sprintf("%s", 1) works there and in 2.0.1 gives a useful error message. On Fri, 11 Feb 2005 ehlers@math.ucalgary.ca wrote:> Full_Name: Peter Ehlers > Version: rw2001pat (2005-02-03) > OS: Win XP > Submission from: (NULL) (136.159.61.115) > > > formatC(1, flag="s") crashes Rgui. > Similarly for flag=[SnZ]. > Stupid input, of course, but I'm error-prone.-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Wolfgang Huber
2005-Feb-14 12:13 UTC
[Rd] sprintf - was formatC with illegal input crashes Rgui (PR#7686)
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
sprintf() in R-devel is now vectorized, re-cycling its arguments, including fmt, as required. On Tue, 15 Feb 2005, Wolfgang Huber wrote:> Hi Peter, > > thanks. Yet my intention was not to discuss whether this could be done at all > (never had any doubts about that), but how it could be done nicely and > conveniently for the application programmer. > > Best regards > Wolfgang > > Peter Dalgaard wrote: >> Wolfgang Huber <huber@ebi.ac.uk> writes: >> >> >>> 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. >> >> >> ...which is of course fixable with either of >> >> >> >>> mapply(sprintf, MoreArgs=list(fmt="%04d %s"), 1:3, "abc") >> >> [1] "0001 abc" "0002 abc" "0003 abc" >> >>> mapply(sprintf, 1:3, "abc", fmt="%04d %s") >> >> [1] "0001 abc" "0002 abc" "0003 abc" >> >>> mapply(sprintf, "%04d %s", 1:3, "abc", USE.NAMES=FALSE) >> >> [1] "0001 abc" "0002 abc" "0003 abc" >> >> (Only the last one is completely failsafe since the first two relies >> on 1:3 not being character: >> >> if (USE.NAMES && length(dots) && is.character(dots[[1]]) && >> is.null(names(answer))) >> names(answer) <- dots[[1]] >> ) > >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595