[R-gregmisc-commits] r2158 - in pkg/gtools: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 13 00:49:50 CEST 2017


Author: warnes
Date: 2017-06-13 00:49:49 +0200 (Tue, 13 Jun 2017)
New Revision: 2158

Modified:
   pkg/gtools/R/capwords.R
   pkg/gtools/man/capwords.Rd
Log:
Add capwords() function to apply standard capitalization rules to a scharacter string.

Modified: pkg/gtools/R/capwords.R
===================================================================
--- pkg/gtools/R/capwords.R	2017-06-06 17:18:49 UTC (rev 2157)
+++ pkg/gtools/R/capwords.R	2017-06-12 22:49:49 UTC (rev 2158)
@@ -1,60 +1,59 @@
-capwords <- function(s, 
-                     strict=FALSE, 
-                     AP=TRUE, 
-                     onlyfirst=FALSE, 
-                     preserveMixed=FALSE, 
-                     sep=" ") 
+capwords <- function(s,
+                     strict=FALSE,
+                     AP=TRUE,
+                     onlyfirst=FALSE,
+                     preserveMixed=FALSE,
+                     sep=" ")
 {
   # worker functions
-  cap <- function(s) 
+  cap <- function(s)
     paste(toupper(substring(s, 1, 1)),
           {
-            s <- substring(s, 2); 
+            s <- substring(s, 2);
             if(strict) tolower(s) else s
           },
           sep = ""
     )
-  
+
   # test if there is a lowercase letter followed by an uppercase letter
-  isMixedCase <- function(s) 
+  isMixedCase <- function(s)
     grepl("[a-z][A-Z]", s)
 
   words <- unlist(strsplit(s, split = sep))
   mixedCaseFlag <- sapply(words, isMixedCase)
-  
+
   # First, capitalize *every* word
   if(!onlyfirst)
   {
-    newWords <- sapply(words, 
-               cap, 
-               USE.NAMES = !is.null(names(retval))
-               ) 
-    
+    newWords <- sapply(words,
+                       cap
+                       )
+
     if(preserveMixed==TRUE)
       newWords[mixedCaseFlag] <- words[mixedCaseFlag]
-    
+
     words <- newWords
   }
-       
+
   # Next (optionally) uncapitalize prepositions and conjunctions
   # recommended by the Associated Press.
-  AP.nocap <- c("a", "an", "and", "at", "but", "by", "for", "in", 
-                "nor", "of", "on", "or", "so", "the", "to", "up", 
+  AP.nocap <- c("a", "an", "and", "at", "but", "by", "for", "in",
+                "nor", "of", "on", "or", "so", "the", "to", "up",
                 "yet")
-  
-  if(AP && !onlyfirst) 
+
+  if(AP && !onlyfirst)
     for(word in AP.nocap)
-      words <- gsub(paste0("^",word,"$"), 
-                            word, 
-                            words, 
+      words <- gsub(paste0("^",word,"$"),
+                            word,
+                            words,
                             ignore.case=TRUE)
-                
-  
+
+
   # Finally, ensure that the first word is capitalized
   if(length(words)>0 && mixedCaseFlag[1]==FALSE)
     words[1] <- cap(words[1])
-  
+
   retval <- paste(words, collapse=sep)
-  
+
   retval
-}
\ No newline at end of file
+}

Modified: pkg/gtools/man/capwords.Rd
===================================================================
--- pkg/gtools/man/capwords.Rd	2017-06-06 17:18:49 UTC (rev 2157)
+++ pkg/gtools/man/capwords.Rd	2017-06-12 22:49:49 UTC (rev 2158)
@@ -7,18 +7,20 @@
 This function capitalizes words for use in titles
 }
 \usage{
-capwords(s, strict=FALSE, AP=TRUE, firstonly=FALSE, 
+capwords(s, strict=FALSE, AP=TRUE, onlyfirst=FALSE,
          preserveMixed=FALSE, sep=" ")
 }
 \arguments{
   \item{s}{character string to be processed}
-  \item{strict}{Logical, remove all additional capitalization}
+  \item{strict}{Logical, remove all additional capitalization.}
   \item{AP}{Logical, apply the Associated Press (AP) rules for
     prepositions and conjunctions that should not be capitalized
     in titles.}
+  \item{onlyfirst}{Logical, only capitalize the first word.}
   \item{preserveMixed}{Logical, preserve the capitalization
-    mixed-case words containing an upper-case letter after a 
+    mixed-case words containing an upper-case letter after a
     lower-case letter.}
+  \item{sep}{Character string, word separator}
 }
 \details{
 This function separates the provided character string into separate words using \code{sep} as the word separator.  If \code{firstonly==TRUE}, it then capitalizes the first letter the first word, otherwise (the default), it capitalizes the first letter of every word.  If \code{AP==TRUE}, it then un-capitalizes words in the Associated Press's (AP) list of prepositions and conjunctions should not be capitalized in titles.  Next, it capitalizes the first word. It then re-joins the words using the specified separator.
@@ -29,10 +31,10 @@
 A character scalar containing the capitalized words.
 }
 \references{
-Fogarty, Mignon. “Capitalizing Titles: Which words should you capitalize?” Grammar Girl's Quick and Dirty Tips for Better Writing. 9 Jun. 2011. Quick and Dirty Tips Website. Accessed 22 April 2016 \url{http://www.quickanddirtytips.com/education/grammar/capitalizing-titles}
+Fogarty, Mignon. Capitalizing Titles: "Which words should you capitalize? Grammar Girl's Quick and Dirty Tips for Better Writing. 9 Jun. 2011. Quick and Dirty Tips Website." Accessed 22 April 2016 \url{http://www.quickanddirtytips.com/education/grammar/capitalizing-titles}
 }
 \author{
-Gregory R. Warnes \email{greg at warnes.net} based on code from the \code{\link[base]{chartr}} manual page, 
+Gregory R. Warnes \email{greg at warnes.net} based on code from the \code{\link[base]{chartr}} manual page,
 and \code{\link[taxize]{capwords}}
 in the taxize package.
 }



More information about the R-gregmisc-commits mailing list