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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Apr 7 02:43:01 CEST 2018


Author: andybunn
Date: 2018-04-07 02:43:01 +0200 (Sat, 07 Apr 2018)
New Revision: 1091

Added:
   pkg/dplR/R/csv2rwl.R
   pkg/dplR/man/csv2rwl.Rd
Modified:
   pkg/dplR/ChangeLog
   pkg/dplR/NAMESPACE
   pkg/dplR/R/read.rwl.R
   pkg/dplR/man/read.rwl.Rd
Log:
Adding new function to read csv files in as rwl objects. Also adding that capability into read.rwl. Mikko should see if the error checks etc pass muster.


Modified: pkg/dplR/ChangeLog
===================================================================
--- pkg/dplR/ChangeLog	2018-04-06 04:48:45 UTC (rev 1090)
+++ pkg/dplR/ChangeLog	2018-04-07 00:43:01 UTC (rev 1091)
@@ -2,6 +2,14 @@
 
 - Note that Darwin Alexander Pucha Cofrep has been added as a developer to work on plotRings() etc.
 
+File: csv2rwl.R
+----------------
+Adding new function to read csv files in as rwl objects. Also adding that capability into read.rwl. Mikko should see if the error checks etc pass muster.
+
+File: read.rwl.R
+----------------
+Adding read2csv into read.rwl both as a format specification and as an auto detect. Mikko should see if the error checks etc pass muster.
+
 File: time.rwl.R
 ----------------
 

Modified: pkg/dplR/NAMESPACE
===================================================================
--- pkg/dplR/NAMESPACE	2018-04-06 04:48:45 UTC (rev 1090)
+++ pkg/dplR/NAMESPACE	2018-04-07 00:43:01 UTC (rev 1091)
@@ -52,7 +52,7 @@
        write.tucson, plot.rwl, interseries.cor, summary.rwl, plot.crn,
        insert.ring, delete.ring, xskel.ccf.plot, xskel.plot, latexify,
        latexDate, rasterPlot, treeMean, rwl.report, print.rwl.report, 
-       plotRings,time.rwl,time.crn)
+       plotRings,time.rwl,time.crn,csv2rwl)
 
 S3method(print, redfit)
 S3method(plot, rwl)

Added: pkg/dplR/R/csv2rwl.R
===================================================================
--- pkg/dplR/R/csv2rwl.R	                        (rev 0)
+++ pkg/dplR/R/csv2rwl.R	2018-04-07 00:43:01 UTC (rev 1091)
@@ -0,0 +1,8 @@
+`csv2rwl` <- function(fname,...)
+{
+  dat <- read.table(fname, header=TRUE, sep=",",...)
+  rownames(dat) <- as.character(dat[,1])
+  dat <- dat[,-1]
+  class(dat) <- c("rwl","data.frame")
+  dat
+}


Property changes on: pkg/dplR/R/csv2rwl.R
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: pkg/dplR/R/read.rwl.R
===================================================================
--- pkg/dplR/R/read.rwl.R	2018-04-06 04:48:45 UTC (rev 1090)
+++ pkg/dplR/R/read.rwl.R	2018-04-07 00:43:01 UTC (rev 1091)
@@ -1,57 +1,69 @@
 read.rwl <-
-    function(fname,
-             format=c("auto", "tucson", "compact", "tridas", "heidelberg"),
-             ...)
-{
+  function(fname,
+           format=c("auto", "tucson", "compact", "tridas", "heidelberg", "csv"),
+           ...)
+  {
     switch(match.arg(format),
            auto = {
-               f <- file(fname,"r")
-               l1 <- readLines(f, n=1)
-               if(length(l1) == 0){
-                   close(f)
-                   stop("file is empty")
-               }
-               ## A rough test for a compact format file
-               if(grepl("[1-9][0-9]*\\([1-9][0-9]*F[1-9][0-9]*\\.0\\)~ *$",
-                        l1)){
-                   cat(gettext("Detected a DPL compact format file.\n",
+             cat(gettext("Attempting to automatically detect format.\n",
+                         domain="R-dplR"))
+             f <- file(fname,"r")
+             l1 <- readLines(f, n=1)
+             if(length(l1) == 0){
+               close(f)
+               stop("file is empty")
+             }
+             ## A rough test for a compact format file
+             if(grepl("[1-9][0-9]*\\([1-9][0-9]*F[1-9][0-9]*\\.0\\)~ *$",
+                      l1)){
+               cat(gettext("Detected a DPL compact format file.\n",
+                           domain="R-dplR"))
+               close(f)
+               read.compact(fname, ...)
+             } 
+             else if(grepl("^HEADER:$", l1)){ # Heidelberg test
+               cat(gettext("Detected a Heidelberg format file.\n",
+                           domain="R-dplR"))
+               close(f)
+               read.fh(fname, ...)
+             } 
+             else { # two tests for tridas
+               ## A rough test for a TRiDaS file
+               if(grepl("<tridas>", l1)){
+                 cat(gettext("Detected a TRiDaS file.\n",
+                             domain="R-dplR"))
+                 close(f)
+                 read.tridas(fname, ...)
+               } 
+               else {
+                 ## <tridas> may be preceded by an XML
+                 ## declaration, comments, etc. Therefore, if
+                 ## the first line did not contain <tridas>, we
+                 ## read a "reasonable number" of additional
+                 ## lines, and try to detect <tridas> in those.
+                 more.lines <- readLines(f, n=20)
+                 close(f)
+                 if(any(grepl("<tridas>", more.lines))){
+                   cat(gettext("Detected a TRiDaS file.\n",
                                domain="R-dplR"))
-                   close(f)
-                   read.compact(fname, ...)
-               } else if(grepl("^HEADER:$", l1)){ # Heidelberg test
-                   cat(gettext("Detected a Heidelberg format file.\n",
+                   read.tridas(fname, ...)
+                 } 
+                 else if(grepl(",", l1)){
+                   cat(gettext("Detected a csv file.\n",
                                domain="R-dplR"))
-                   close(f)
-                   read.fh(fname, ...)
-               } else {
-                   ## A rough test for a TRiDaS file
-                   if(grepl("<tridas>", l1)){
-                       cat(gettext("Detected a TRiDaS file.\n",
-                                   domain="R-dplR"))
-                       close(f)
-                       read.tridas(fname, ...)
-                   } else {
-                       ## <tridas> may be preceded by an XML
-                       ## declaration, comments, etc. Therefore, if
-                       ## the first line did not contain <tridas>, we
-                       ## read a "reasonable number" of additional
-                       ## lines, and try to detect <tridas> in those.
-                       more.lines <- readLines(f, n=20)
-                       close(f)
-                       if(any(grepl("<tridas>", more.lines))){
-                           cat(gettext("Detected a TRiDaS file.\n",
-                                       domain="R-dplR"))
-                           read.tridas(fname, ...)
-                       } else {
-                           cat(gettext("Assuming a Tucson format file.\n",
-                                       domain="R-dplR"))
-                           read.tucson(fname, ...)
-                       }
-                   }
+                   csv2rwl(fname, ...)
+                 }
+                 else { # Settle for tucson if none of the above pan out
+                   cat(gettext("Assuming a Tucson format file.\n",
+                               domain="R-dplR"))
+                   read.tucson(fname, ...)
+                 }
                }
+             }
            },
+           tucson = read.tucson(fname, ...),
            compact = read.compact(fname, ...),
+           tridas = read.tridas(fname, ...),
            heidelberg = read.fh(fname, ...),
-           tridas = read.tridas(fname, ...),
-           tucson = read.tucson(fname, ...))
-}
+           csv = csv2rwl(fname, ...))
+  }

Added: pkg/dplR/man/csv2rwl.Rd
===================================================================
--- pkg/dplR/man/csv2rwl.Rd	                        (rev 0)
+++ pkg/dplR/man/csv2rwl.Rd	2018-04-07 00:43:01 UTC (rev 1091)
@@ -0,0 +1,50 @@
+\encoding{UTF-8}
+\name{csv2rwl}
+\alias{csv2rwl}
+\title{ Read Ring Width File from CSV }
+\description{
+  This function reads in a file of ring widths (.rwl) from a text file with comma separated values (csv).
+}
+\usage{
+csv2rwl(fname,...)
+}
+\arguments{
+
+  \item{fname}{ a \code{character} vector giving the file name of the
+    csv file. }
+
+  \item{\dots}{ other arguments passed to \code{\link{read.table}}. }
+}
+\details{
+This is a simple wrapper to \code{\link{read.table}} that reads in a text file with ring-width series in columns and the the years as rows. The file should have the first column contain the years and each subsequent column contain a series. The series names should be in the first row of the file. 
+
+Note that this is a rudimentary convenience function that isn't doing anything sophisticated. It reads in a file, assigns the years to the row names and sets the class of the object to \code{c("rwl","data.frame")} which allows \code{dplR} to recognize it. 
+
+Although arguments can be passed to \code{\link{read.table}}, this is not designed to be a  flexible function. As is the philosophy with \code{dplR}, modifying the code is easy should the user wish to read in a different style of text file (e.g., tab delimited). Typing \code{csv2rwl} at the \code{R} prompt will get the user started. 
+
+}
+\value{
+  Function returns an object of class
+  \code{c("rwl", "data.frame")} with the series in columns and the years
+  as rows.  The series \acronym{ID}s are the column names and the years
+  are the row names.
+}
+\author{ Andy Bunn }
+\seealso{ \code{\link{read.rwl}} }
+\examples{
+library(utils)
+data(ca533)
+# write out a rwl file in a format that csv2rwl will understand
+tm <- time(ca533)
+foo <- data.frame(tm,ca533)
+# this is the temp file where foo will be written
+tmpName <- tempfile()
+write.csv(foo,file=tmpName,row.names=FALSE)
+# read it back in using csv2rwl
+bar <- csv2rwl(tmpName)
+# check to see if identical
+identical(ca533,bar)
+# delete temp file
+unlink(tmpName)
+}
+\keyword{ IO }


Property changes on: pkg/dplR/man/csv2rwl.Rd
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: pkg/dplR/man/read.rwl.Rd
===================================================================
--- pkg/dplR/man/read.rwl.Rd	2018-04-06 04:48:45 UTC (rev 1090)
+++ pkg/dplR/man/read.rwl.Rd	2018-04-07 00:43:01 UTC (rev 1091)
@@ -8,7 +8,7 @@
 }
 \usage{
 read.rwl(fname,
-         format = c("auto", "tucson", "compact", "tridas", "heidelberg"),
+         format = c("auto", "tucson", "compact", "tridas", "heidelberg", "csv"),
          \dots)
 }
 \arguments{
@@ -17,9 +17,9 @@
     rwl file. }
 
   \item{format}{ a \code{character} vector giving the format.  This must
-    be \code{"auto"} (automatic detection), \code{"tucson"},
-    \code{"compact"}, \code{"tridas"} or \code{"heidelberg"}.  Automatic
-    format detection is the default. }
+    be \code{"auto"} (rough automatic detection), \code{"tucson"},
+    \code{"compact"}, \code{"tridas"}, \code{"heidelberg"} or \code{"csv"}.  Automatic
+    format detection is the default but failable. }
 
   \item{\dots}{ arguments specific to the function implementing the
     operation for the chosen format. }
@@ -30,7 +30,7 @@
   read operation.
 }
 \value{
-  If a \code{"tucson"}, \code{"compact"} or \code{"heidelberg"} file is
+  If a \code{"tucson"}, \code{"compact"}, \code{"heidelberg"}, \code{"csv"} file is
   read (even through \code{"auto"}), returns an object of class
   \code{c("rwl", "data.frame")} with the series in columns and the years
   as rows.  The series \acronym{ID}s are the column names and the years
@@ -42,5 +42,6 @@
 }
 \author{ Mikko Korpela }
 \seealso{ \code{\link{read.tucson}}, \code{\link{read.compact}},
-  \code{\link{read.tridas}}, \code{\link{read.fh}}, \code{\link{write.rwl}} }
+  \code{\link{read.tridas}}, \code{\link{read.fh}}, \code{\link{csv2rwl}}, 
+  \code{\link{write.rwl}} }
 \keyword{ IO }



More information about the Dplr-commits mailing list