[Dplr-commits] r862 - in pkg/dplR: . R man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat May 10 23:12:04 CEST 2014


Author: mvkorpel
Date: 2014-05-10 23:12:04 +0200 (Sat, 10 May 2014)
New Revision: 862

Added:
   pkg/dplR/R/latexify.R
   pkg/dplR/man/latexDate.Rd
   pkg/dplR/man/latexify.Rd
Modified:
   pkg/dplR/ChangeLog
   pkg/dplR/DESCRIPTION
   pkg/dplR/NAMESPACE
   pkg/dplR/vignettes/intro-dplR.Rnw
   pkg/dplR/vignettes/timeseries-dplR.Rnw
   pkg/dplR/vignettes/xdate-dplR.Rnw
Log:
Helper functions for vignettes:
* latexDate(): Use this to fix date shown in document at Sweave / knit time
* latexify(): When you are not directly in control of 'expr' in
  '\Sexpr{expr}', use '\Sexpr{latexify(expr)}', because as.character(expr)
  may include characters that cannot be used in LaTeX code unescaped.


Modified: pkg/dplR/ChangeLog
===================================================================
--- pkg/dplR/ChangeLog	2014-05-06 14:13:08 UTC (rev 861)
+++ pkg/dplR/ChangeLog	2014-05-10 21:12:04 UTC (rev 862)
@@ -1,11 +1,21 @@
 * CHANGES IN dplR VERSION 1.6.1
 
+File: NAMESPACE
+---------------
+
+- Added latexify() and latexDate() to export list
+
 File: common.interval.R
 -----------------------
 
 - Bug fix: make.plot=TRUE threw an error when input data.frame had leading
   or trailing all-NA rows
 
+File: latexify.R
+----------------
+
+- New utility functions latexify() and latexDate() for use in vignettes
+
 Files: rcompact.c, readloop.c
 -----------------------------
 

Modified: pkg/dplR/DESCRIPTION
===================================================================
--- pkg/dplR/DESCRIPTION	2014-05-06 14:13:08 UTC (rev 861)
+++ pkg/dplR/DESCRIPTION	2014-05-10 21:12:04 UTC (rev 862)
@@ -3,7 +3,7 @@
 Type: Package
 Title: Dendrochronology Program Library in R
 Version: 1.6.1
-Date: 2014-05-06
+Date: 2014-05-10
 Authors at R: c(person("Andy", "Bunn", role = c("aut", "cph",
         "cre", "trl"), email = "andy.bunn at wwu.edu"), person("Mikko",
         "Korpela", role = c("aut", "trl")), person("Franco", "Biondi",

Modified: pkg/dplR/NAMESPACE
===================================================================
--- pkg/dplR/NAMESPACE	2014-05-06 14:13:08 UTC (rev 861)
+++ pkg/dplR/NAMESPACE	2014-05-10 21:12:04 UTC (rev 862)
@@ -38,7 +38,8 @@
        tridas.vocabulary, uuid.gen, wavelet.plot, wc.to.po,
        write.compact, write.crn, write.rwl, write.tridas,
        write.tucson, plot.rwl, interseries.cor, summary.rwl,
-       plot.crn, insert.ring, delete.ring, xskel.ccf.plot, xskel.plot)
+       plot.crn, insert.ring, delete.ring, xskel.ccf.plot, xskel.plot,
+       latexify, latexDate)
 
 S3method(print, redfit)
 S3method(plot, rwl)

Added: pkg/dplR/R/latexify.R
===================================================================
--- pkg/dplR/R/latexify.R	                        (rev 0)
+++ pkg/dplR/R/latexify.R	2014-05-10 21:12:04 UTC (rev 862)
@@ -0,0 +1,81 @@
+### Helpers for vignettes.
+
+## Return a string representing the given date(s) (default: current date)
+## in the format used by \today in LaTeX.
+## Example: latexDate("2013-12-06") returns "December 6, 2013"
+latexDate <- function(x = Sys.Date(), ...) {
+    ltDate <- as.POSIXlt(x, ...)
+    sprintf("%s %d, %d",
+            month.name[ltDate[["mon"]] + 1],
+            ltDate[["mday"]],
+            1900 + ltDate[["year"]])
+}
+
+## Usage: \Sexpr{latexify(string_produced_by_R_code)}
+##
+## Make arbitrary character() vector compatible with LaTeX by escaping
+## special characters, then convert to UTF-8 encoding.  Any formatting
+## (newlines, tabs, etc.) will be lost.  Note that the set of
+## characters actually supported depends on the font, LaTeX engine and
+## set of packages used.
+latexify <- function(x) {
+    y <- as.character(x)
+    ## Kludge for converting from "byte" to the current encoding
+    ## in a way which preserves the hex notation.
+    encBytes <- Encoding(y) == "bytes"
+    if (any(encBytes)) {
+        foo <- character(0) # dummy line
+        tc <- textConnection("foo", "w", local = TRUE)
+        sink(tc)
+        on.exit(sink())
+        on.exit(close(tc), add = TRUE)
+        ## Embedded newlines (if any) in y[encBytes] will not cause
+        ## line breaks with cat().
+        cat(y[encBytes], sep = "\n")
+        y[encBytes] <- foo
+    }
+    ## Convert any sequence of whitespace to a single space.  This
+    ## substitution must be done before control characters because
+    ## newline belongs to both groups.
+    y <- gsub("[[:space:]]+", " ", y)
+    ## Remove control characters
+    y <- gsub("[[:cntrl:]]", "", y)
+    ## Escape LaTeX special characters.
+    ## Source: Scott Pakin (2009) The Comprehensive LaTeX Symbol List.
+    ## Accessible through "texdoc symbols".
+    ## Particularly section 8.6 "ASCII and Latin 1 quick reference".
+    ##
+    ## The order of the elements in the list matters!
+    ## First, { and } are replaced with \{ and \}, respectively.
+    ## Then, \ is replaced with \textbackslash{},
+    ## but not if followed by { or }.
+    ## After that, the order does not matter.
+    substitutions <-
+        list(c("\\{", "\\\\{"),
+             c("\\}", "\\\\}"),
+             c("\\\\(?!(\\{|\\}))", "\\\\textbackslash{}"),
+             c("\\#", "\\\\#"),
+             c("\\$", "\\\\$"),
+             c("%", "\\\\%"),
+             c("\\^", "\\\\^{}"),
+             c("&", "\\\\&"),
+             c("_", "\\\\_"),
+             c("~", "\\\\~{}"),
+             c('"', "\\\\textquotedbl{}"),
+             c("/", "\\\\slash{}"))
+    for (subst in substitutions) {
+        y <- gsub(subst[1], subst[2], y, perl = TRUE)
+    }
+    ## gsub() may have changed encodings. Therefore we check them
+    ## again.
+    encs <- Encoding(y)
+    encLatin <- which(encs == "latin1")
+    if (length(encLatin) > 0) {
+        y[encLatin] <- iconv(y[encLatin], from = "latin1", to = "UTF-8")
+    }
+    encUnknown <- which(encs == "unknown")
+    if (length(encUnknown) > 0) {
+        y[encUnknown] <- iconv(y[encUnknown], to = "UTF-8")
+    }
+    y
+}

Added: pkg/dplR/man/latexDate.Rd
===================================================================
--- pkg/dplR/man/latexDate.Rd	                        (rev 0)
+++ pkg/dplR/man/latexDate.Rd	2014-05-10 21:12:04 UTC (rev 862)
@@ -0,0 +1,32 @@
+\name{latexDate}
+\alias{latexDate}
+\title{
+  Date Conversion to Character in LaTeX Format
+}
+\description{
+  This is a simple convenience function that returns a date in the
+  format used by \samp{\today} in LaTeX.  A possible use case is fixing
+  the date shown in a vignette at weaving time.
+}
+\usage{
+latexDate(x = Sys.Date(), ...)
+}
+\arguments{
+  \item{x}{ any object for which an \code{as.POSIXlt} method exists.
+    Defaults to the current date. }
+  \item{\dots}{ other arguments to \code{as.POSIXlt} }
+}
+\value{
+  A \code{character} vector
+}
+\author{
+  Mikko Korpela
+}
+\examples{
+latexDate()                              # today
+latexDate(Sys.Date() + 5)                # today + 5 days
+latexDate(c("2013-12-06", "2014-09-19")) # fixed dates
+## [1] "December 6, 2013"   "September 19, 2014"
+latexDate(5*60*60*24, origin=Sys.Date()) # today + 5 days
+}
+\keyword{ utilities }

Added: pkg/dplR/man/latexify.Rd
===================================================================
--- pkg/dplR/man/latexify.Rd	                        (rev 0)
+++ pkg/dplR/man/latexify.Rd	2014-05-10 21:12:04 UTC (rev 862)
@@ -0,0 +1,74 @@
+\name{latexify}
+\alias{latexify}
+\title{
+  Convert Character Strings for Use with LaTeX
+}
+\description{
+  Some characters cannot be entered directly into a LaTeX document.
+  This function converts the input \code{character} vector to a form
+  suitable for inclusion in a LaTeX document in text mode.  It can be
+  used together with \samp{\Sexpr} in vignettes.
+}
+\usage{
+latexify(x)
+}
+\arguments{
+  \item{x}{ a \code{character} vector }
+}
+\details{
+
+  The function is intended for use with unformatted inline text.
+  Newlines, tabs and other whitespace characters (\code{"[:space:]"} in
+  \link{regex}) are converted to spaces.  Control character
+  (\code{"[:cntrl:]"}) that are not whitespace are removed.  Other
+  special characters are {, }, \, #, $, \%, ^, &, _, ~, \" and /.  They
+  are converted to the corresponding LaTeX commands.
+
+  Before applying the substitutions described above, input elements with
+  \code{Encoding} set to \code{"bytes"} are printed and the result is
+  captured using the current text encoding.  The result of this
+  intermediate stage is ASCII text where some characters are shown as
+  their byte codes using a hexadecimal pair prefixed with \code{"\\x"}.
+  This set includes tabs, newlines and control characters.  The
+  substitutions are then applied to the intermediate result.
+
+  Input elements with \code{"unknown"} encoding are assumed to be in the
+  current encoding.  These and \code{"latin1"} encoded elements are
+  converted to UTF-8.
+
+  Suggested package loading commands in the document preamble
+  are:\preformatted{\usepackage[T1]{fontenc}    \% required for "
+\usepackage[utf8]{inputenx} \% UTF-8 input encoding
+\input{ix-utf8enc.dfu}      \% more supported characters} 
+  
+}
+\value{
+  A \code{character} vector
+}
+\references{
+  Pakin, S. (2009) The Comprehensive LaTeX Symbol
+  List. \url{http://www.ctan.org/tex-archive/info/symbols/comprehensive}
+}
+\author{
+  Mikko Korpela
+}
+\examples{
+x1 <- "clich\xe9\nma\xf1ana"
+Encoding(x1) <- "latin1"
+x1
+x2 <- x1
+Encoding(x2) <- "bytes"
+x2
+testStrings <-
+    c("different     kinds\nof\tspace",
+      "control\a characters \ftoo",
+      "{braces} and \\\\backslash",
+      '#various$ \%other^ &characters_ ~escaped"/coded',
+      x1,
+      x2)
+latexStrings <- latexify(testStrings)
+## 5th element should be "UTF-8", the rest "unknown"
+Encoding(latexStrings)
+cat(latexStrings, sep="\n")
+}
+\keyword{ utilities }

Modified: pkg/dplR/vignettes/intro-dplR.Rnw
===================================================================
--- pkg/dplR/vignettes/intro-dplR.Rnw	2014-05-06 14:13:08 UTC (rev 861)
+++ pkg/dplR/vignettes/intro-dplR.Rnw	2014-05-10 21:12:04 UTC (rev 862)
@@ -6,12 +6,15 @@
 \input{ix-utf8enc.dfu} % more characters supported
 \title{An introduction to dplR} 
 \author{Andy Bunn \and Mikko Korpela}
+<<echo=FALSE,results=hide>>=
+library(dplR) # latexify(), latexDate()
+@ 
 \hypersetup{
   pdfauthor = {Andy Bunn; Mikko Korpela},
 }
-\date{\footnotesize{$ $Processed with dplR 
+\date{\footnotesize Processed with dplR 
 \Sexpr{packageDescription("dplR", field="Version")}
-in \Sexpr{R.version.string} on \today}}
+in \Sexpr{latexify(R.version.string)} on \Sexpr{latexDate()}}
 
 \begin{document}
 \bibliographystyle{jss}

Modified: pkg/dplR/vignettes/timeseries-dplR.Rnw
===================================================================
--- pkg/dplR/vignettes/timeseries-dplR.Rnw	2014-05-06 14:13:08 UTC (rev 861)
+++ pkg/dplR/vignettes/timeseries-dplR.Rnw	2014-05-10 21:12:04 UTC (rev 862)
@@ -6,12 +6,15 @@
 \input{ix-utf8enc.dfu} % more characters supported
 \title{Time Series Analysis in dplR} 
 \author{Andy Bunn \and Mikko Korpela}
+<<echo=FALSE,results=hide>>=
+library(dplR) # latexify(), latexDate()
+@ 
 \hypersetup{
   pdfauthor = {Andy Bunn; Mikko Korpela},
 }
-\date{\footnotesize{$ $Processed with dplR 
+\date{\footnotesize Processed with dplR 
 \Sexpr{packageDescription("dplR", field="Version")}
-in \Sexpr{R.version.string} on \today}}
+in \Sexpr{latexify(R.version.string)} on \Sexpr{latexDate()}}
 
 \begin{document}
 \bibliographystyle{jss}

Modified: pkg/dplR/vignettes/xdate-dplR.Rnw
===================================================================
--- pkg/dplR/vignettes/xdate-dplR.Rnw	2014-05-06 14:13:08 UTC (rev 861)
+++ pkg/dplR/vignettes/xdate-dplR.Rnw	2014-05-10 21:12:04 UTC (rev 862)
@@ -6,12 +6,15 @@
 \input{ix-utf8enc.dfu} % more characters supported
 \title{Crossdating in dplR} 
 \author{Andy Bunn \and Mikko Korpela}
+<<echo=FALSE,results=hide>>=
+library(dplR) # latexify(), latexDate()
+@ 
 \hypersetup{
   pdfauthor = {Andy Bunn; Mikko Korpela},
 }
-\date{\footnotesize{$ $Processed with dplR 
+\date{\footnotesize Processed with dplR 
 \Sexpr{packageDescription("dplR", field="Version")}
-in \Sexpr{R.version.string} on \today}}
+in \Sexpr{latexify(R.version.string)} on \Sexpr{latexDate()}}
 
 \begin{document}
 \bibliographystyle{jss}



More information about the Dplr-commits mailing list