## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  message = FALSE, warning = FALSE,
  fig.width = 7, fig.height = 4.5, fig.align = "center",
  dev = "png", dpi = 96
)

## ----load---------------------------------------------------------------------
library(dplR)

## ----read, eval=FALSE---------------------------------------------------------
# dat <- read.rwl("mysite.rwl", format = "tucson")

## ----data---------------------------------------------------------------------
data(co021)
class(co021)
dim(co021)
co021[1:5, 1:4]

## ----report-------------------------------------------------------------------
rwl.report(co021)

## ----describe-----------------------------------------------------------------
head(summary(co021))
plot(co021, plot.type = "spag")

## ----detrend-one--------------------------------------------------------------
x <- co021[, "641114"]
names(x) <- rownames(co021)
x.rwi <- detrend.series(x, method = c("Spline", "ModNegExp"),
                        make.plot = TRUE)

## ----detrend------------------------------------------------------------------
co021.rwi <- detrend(co021, method = "Spline")

## ----rwi-summary--------------------------------------------------------------
summary(co021.rwi)

## ----rwi-image, fig.height=6--------------------------------------------------
plot(co021.rwi, plot.type = "image")

## ----rwi-image-mean, fig.height=6---------------------------------------------
plot(detrend(co021, method = "Mean"), plot.type = "image")

## ----chron--------------------------------------------------------------------
co021.crn <- chron(co021.rwi)
tail(co021.crn)
plot(co021.crn, add.spline = TRUE, nyrs = 32)

## ----plant--------------------------------------------------------------------
dat <- co021
x <- dat[, "641143"]
names(x) <- rownames(dat)
dat[, "641143"] <- delete.ring(x, year = 1500)

## ----crs, fig.height=6--------------------------------------------------------
crs <- corr.rwl.seg(dat, seg.length = 50, pcrit = 0.01,
                    lag.max = 10, label.cex = 0.7)

## ----best-lag-----------------------------------------------------------------
ok <- !is.na(crs$best.lag["641143", ])
crs$best.lag["641143", ok]

## ----ccf, fig.height=6--------------------------------------------------------
ccf <- ccf.series.rwl(rwl = dat[, colnames(dat) != "641143"],
                      series = dat[, "641143"],
                      series.yrs = as.numeric(rownames(dat)),
                      seg.length = 50, bin.floor = 50)

## ----xdate-report, results="hide"---------------------------------------------
rpt <- xdate.report(dat, title = "co021 with a planted fault")
rpt

## ----report-sections, include=FALSE-------------------------------------------
## Split the Markdown version of the report into its sections so the
## vignette can show a couple of them as tables. Headings become bold
## labels, which keeps them out of the table of contents and out of
## pandoc's section structure.
md <- format(rpt, type = "markdown")
md <- sub("^#{1,2} (.*)$", "**\\1**", md)
sec <- cumsum(grepl("^\\*\\*", md))
sections <- split(md, sec)
names(sections) <- sapply(sections, function(s) gsub("\\*", "", s[1]))
show.section <- function(name) {
    cat(sections[[name]], sep = "\n")
    cat("\n")
}

## ----report-summary, echo=FALSE, results="asis"-------------------------------
show.section("Summary")

## ----report-flags, echo=FALSE, results="asis"---------------------------------
show.section("Flagged segments")

## ----flagged------------------------------------------------------------------
rpt$flagged[, c("series", "from", "to", "flag", "best.lag", "gain")]

## ----report-full, echo=FALSE, results="asis"----------------------------------
cat(md, sep = "\n")

## ----write-report, eval=FALSE-------------------------------------------------
# write.xdate.report(rpt, "co021-report.html")

