Pfaff, Bernhard
2003-Jan-09 16:23 UTC
[R] Elements of a character vector in different colours?
Dear R-List, is it possible to format elements of a character vector in different colors and how would this be achieved? Problem: Suppose, you have a model for forecasting purposes. Beside the forecast [variable: 'forecast' in the example below] a forecast interval is also computed [variable: 'int' in the example below. The forecast should now be translated in verbal terms if the forecast interval is below or above a certain threshold value [in the example below, this value is set to 50]. After evaluation the object text should be formatted in either "green", or "red", or "black". outcome <- c("positive","negative","neutral") forecast <- 60 int <- c(55,65) # text <- outcome[3] text[forecast > 50 & int[1] > 50] <- outcome[1] text[forecast < 50 & int[2] < 50] <- outcome[2] text I tried the following outcome <- c("positive","negative","neutral")[col=c("green","red","black"] without success, i.e. three times "na" formatted in green is returned. I did also check the former help-emails without finding any pointers to solve my problem. As a side note: the object 'text' will be used in the supplementary package R2HTML. I want to avoid programming the conditional colouring with ASP by reading in an ascii-file that holds the value of 'text' and hence not using R2HTML at all. R: 1.61 OS: Windows NT i386 Any help or hints are warmly appreciated. Bernhard ---------------------------------------------------------------------- If you have received this e-mail in error or wish to read our e-mail disclaimer statement and monitoring policy, please refer to http://www.drkw.com/disc/email/ or contact the sender. ----------------------------------------------------------------------
jasont@indigoindustrial.co.nz
2003-Jan-09 22:53 UTC
[R] Elements of a character vector in different colours?
> is it possible to format elements of a character vector in different colors > and how would this be achieved?On graphs, with the text() command. Colour isn't an inherient property of a character vector. ..> outcome <- c("positive","negative","neutral") > forecast <- 60 > int <- c(55,65) > # > text <- outcome[3] > text[forecast > 50 & int[1] > 50] <- outcome[1] > text[forecast < 50 & int[2] < 50] <- outcome[2] > textBy the way, it's a good idea to check if your variable names conflict with function names - after you've done the above, try> conflicts(text)> I tried the following > > outcome <- c("positive","negative","neutral")[col=c("green","red","black"] > > without success, i.e. three times "na" formatted in green is returned.Nope. A syntax error is returned. Cut-and-paste error? ;) I think you'll find the word "green" in the above, and the colour green in the NA printout is a coincidence. Try for yourself, and use "purple", or "volvo", or "fish" instead of "green". Looks like you're a bit shaky on R's vector indexing syntax, and what that means. Text in R is just a vector of characters - there is no formatting, font, colour, weight, or hyperlink information stored in it. If you want to supply colour information, you'll have to provide it some other way. I've never used R2HTML, so I can't help you there. Hope that clears a few things up. Jason