[R-gregmisc-users] SF.net SVN: r-gregmisc:[1499] trunk/gdata
Brought to you by:
warnes
From: <wa...@us...> - 2011-09-02 17:24:55
|
Revision: 1499 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1499&view=rev Author: warnes Date: 2011-09-02 17:24:49 +0000 (Fri, 02 Sep 2011) Log Message: ----------- Add 'centerText' function to center text strings for a specified width. Added Paths: ----------- trunk/gdata/R/centerText.R trunk/gdata/man/centerText.Rd Added: trunk/gdata/R/centerText.R =================================================================== --- trunk/gdata/R/centerText.R (rev 0) +++ trunk/gdata/R/centerText.R 2011-09-02 17:24:49 UTC (rev 1499) @@ -0,0 +1,16 @@ +## Function to center text strings for display on the text console +## by prepending the necessary number of spaces to each element. +centerText <- function(x, width=getOption("width")) + { + retval <- vector(length=length(x), mode="character") + for( i in 1:length(x) ) + { + text <- trim(x[i]) + textWidth <- nchar(text) + nspaces <- floor((width - textWidth)/2) + spaces <- paste( rep(" ",nspaces), sep="", collapse="") + retval[i] <- paste( spaces, text, sep="", collapse="\n" ) + } + retval + } + Added: trunk/gdata/man/centerText.Rd =================================================================== --- trunk/gdata/man/centerText.Rd (rev 0) +++ trunk/gdata/man/centerText.Rd 2011-09-02 17:24:49 UTC (rev 1499) @@ -0,0 +1,47 @@ +\name{centerText} +\alias{centerText} +\title{ +Center Text Strings +} +\description{ +Function to center text strings for display on the text console +by prepending the necessary number of spaces to each element. +} +\usage{ +centerText(x, width = getOption("width")) +} +\arguments{ + \item{x}{Character vector containing text strings to be centered.} + \item{width}{Desired display width. Defaults to the R display width + given by \code{getOption("width")}. } +} +\details{ + Each element will be centered individually by prepending the + necessary number of spaces to center the text in the specified + display width assuming a fixed width font. +} +\value{ +Vector of character strings. +} +\author{ +Gregory R. Warnes \email{gr...@wa...} +} +\seealso{ + \code{\link[base]{strwrap}} +} +\examples{ +cat(centerText("One Line Test"), "\n\n") + +mText <-c("This", "is an example", + " of a multiline text ", + "with ", + " leading", + " and trailing ", + "spaces.") +cat("\n", centerText(mText), "\n", sep="\n") +} +% Add one or more standard keywords, see file 'KEYWORDS' in the +% R documentation directory. +\keyword{manip} +\keyword{character} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |