---
title: "Data Processing"
output:
html_document:
theme: flatly
highlight: tango
toc: TRUE
toc_depth: 4 # Define the depth of the TOC (increase as needed)
toc_float:
collapsed: TRUE # Whether the TOC starts collapsed
smooth_scroll: TRUE # Smooth scrolling when a TOC entry is clicked
self_contained: true # standalone HTML
keep_md: false # don’t save intermediate Markdown
fontsize: 12pt # Set font size
linkcolor: blue # Hyperlink color (for PDF output)
vignette: >
%\VignetteIndexEntry{Data Processing}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup_general, include = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
collapse = TRUE,
comment = "#>"
)
```
```{r setup_lib, include=FALSE}
if (!requireNamespace("htmltools", quietly = TRUE)) {
stop("Package 'htmltools' is required to run this function. Please install it.")
}
library(growthTrendR)
library(data.table)
library(ggplot2)
library(sf)
```
# 1. data processing
```{r data_loading}
# Example: load a dataset included in the package
# ring measurement
dt.samples <- fread(system.file("extdata", "dt.samples.csv", package = "growthTrendR"))
# formatting the users' data conformed to CFS-TRenD data structure
dt.samples_trt <- CFS_format(data = list(dt.samples, 39:68), usage = 1, out.csv = NULL)
class(dt.samples_trt)
# save it to extdata for further use
# saveRDS(dt.samples_trt, "inst/extdata/dt.samples_trt.rds")
```
### **arguments of CFS_format() function:**
**data**
All information should be provided in a single file in wide format, with metadata first, followed by the ring-width measurements (in mm).
The column names for the ring-width measurements can follow two formats to indicate the year of measurement: Directly as the year (e.g., 1980) or Prefixed with a character (e.g., X1980).
It is highly recommended that the ring-width measurement columns are ordered by year and consecutive in the dataset, as the column indices will be used as input for the function CFS_format().
data = list (dt.samples, 39:68)), the second item refers to the column indices
**usage**
If users intend to submit their data to the CFS-TRenD online repository, set usage = 1 in the function. This will enable the function to format the data structure and perform detailed checks, including column names, geographic coordinates, species, and other requirements to conform to the CFS-TRenD collection standards.
Otherwise, use usage = 2 to perform a reduced checking procedure, which still builds the CFS-TRenD structure but skips some of the detailed validations.
**out.csv**
if user wants to export the processed tables in csv format, specify the folder here. the default is NULL.
**Note:** Running the function CFS_format() is the first and mandatory step before using any other functions in the growthTrendR package. The data provided in this tutorial is already prepared to run the vignette; in practice, users may need to add or modify their own data based on the messages generated by the function.
# 2. generate data report:
The data report provides an overview of the tree ring data’s quality and characteristics at four levels: project, project-species, project-species-site, and project-species-radii, including the quality assessment at site and radii levels with the default parameters.
More details on quality assessment will be presented next section.
```{r data_report_present, eval=FALSE, message = FALSE, warning = FALSE, results = 'hide'}
generate_report(robj = dt.samples_trt, qa.label_data = "demo-samples ", data_report.reports_sel = c(1,2,3,4), qa.min_nseries = 5, scale.max_dist_km = 200, scale.N_nbs = 2)
```
```{r data_report_eval, include=FALSE, message = FALSE, warning = FALSE, results = 'hide'}
outfile_data <- tempfile(fileext = ".html")
generate_report(robj = dt.samples_trt, qa.label_data = "demo-samples ", data_report.reports_sel = c(1,2,3,4), qa.min_nseries = 5, scale.max_dist_km = 200, scale.N_nbs = 2, output_file = outfile_data)
```
#### **arguments of the generate_report() function:**
**robj **
The input for the data report is the output of the CFS_format() function, which assigns the class "cfs_format" to the resulting object.
**qa.label_data**
A short description of the input dataset. This text will appear in the report as the data source for the generated figures.
**data_report.reports_sel **
This argument specifies the level of data summaries to be included in the reports. Valid options are 1, 2, 3, or 4, each corresponding to one of the four available report types.
In this tutorial, we demonstrate only the project–species level summary.
**output_file **
This argument allows users to export the HTML-formatted report to a specified location by providing a folder and filename (e.g., "path/to/report.html"). If left as NULL (default), the report will not be saved to disk and will instead open directly in the browser for viewing.
```{r data_report_inclu, echo = FALSE, results = 'asis'}
cat('