---
title: "Introduction to Fluxtools"
author: "Kesondra Key"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to Fluxtools}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup}
library(fluxtools)
```

# Overview

**fluxtools** is an R package that provides an interactive Shiny‐based QA/QC environment for data in the AmeriFlux BASE format. In just a few clicks, you can:

1. Upload eddy covariance data in a .csv format (AmeriFlux standard naming and timestamp conventions)
2. Visualize any two numeric columns against time (or each other)
3. Highlight statistical outliers (±σ from a linear fit) and add them to your point-removal R code
4. Manually select and remove data points via a lasso or box. Selecting these adds to the accumulated removal code
6. Copy and paste the generated code into your own R script for reproducible QA/QC
7. Download a “cleaned” CSV with excluded values (using "apply removals") set to `NA` and an R script for reproducibility

This vignette shows you how to install, launch, and use the main Shiny app—`run_flux_qaqc()`—and walks through a typical workflow.

---

# Installation

You can install **fluxtools** from CRAN, or directly from GitHub:

```r
# Install from CRAN 
install.packages("fluxtools")

# Install from GitHub
library(devtools) 
devtools::install_github("kesondrakey/fluxtools")

```

# Launching the Shiny App
Load **fluxtools** and launch the QA/QC application:

```r
library(fluxtools)

# Add the UTC offset for your flux tower site (e.g., UTC-5 for EST)
run_flux_qaqc(-5)
```


 Example workflow

1. **Upload**: Select your AmeriFlux-style CSV (e.g., `US_VT1_HH_202401010000_202501010000.csv`). Files can be up to 100 MB

2. **Choose Year(s)**: By default “all” is selected, but you can subset to specific years

3. **Choose variables**: `TIMESTAMP_START` is on the x-axis by default. Change the y-axis to your variable of interest (e.g., `FC_1_1_1`). The generated R code focuses on removing the y-axis variable

4. **Select data**: Use the box or lasso to select points. This populates the “Current” code box with something like:

   ```r
   df <- df %>%
     mutate(
       FC_1_1_1 = case_when(
         TIMESTAMP_START == '202401261830' ~ NA_real_,
         TIMESTAMP_START == '202401270530' ~ NA_real_,
         …
         TRUE ~ FC_1_1_1
       )
     )
   ```
   
5. **Flag data and Accumulate code**: With points still selected, click “Flag data.” Selected points turn orange, and code is appended to the “Accumulated” box, allowing multiple selections per session.

6. **Unflag data**: Use the box or lasso to de-select points and remove from the Accumulated code box.
   
7. **Clear Selection**: To reset all selections from the current y-variable, click "Clear Selection" to reset the current view.
 
8. **Switch variables**: Change y to any other variable (e.g., `SWC_1_1_1`) and select more points. Click “Flag data” Code for both variables to appear:

 ```r
   df <- df %>%
     mutate(
       FC_1_1_1 = case_when(
         TIMESTAMP_START == '202401261830' ~ NA_real_,
         TIMESTAMP_START == '202401270530' ~ NA_real_,
         …
         TRUE ~ FC_1_1_1
       )
     )

   df <- df %>%
     mutate(
       SWC_1_1_1 = case_when(
         TIMESTAMP_START == '202403261130' ~ NA_real_,
         TIMESTAMP_START == '202403270800' ~ NA_real_,
         …
         TRUE ~ SWC_1_1_1
       )
     )
   ```
   
9. **Compare variables**: Change to variables you would like to compare (e.g., change y to `TA_1_1_1` and x to `T_SONIC_1_1_1`). The app computes an R² via simple linear regression. The top R² is based on points before removals, and once data is selected, a second R² will pop up - calculating the linear regression assuming the selected points have been removed

10. **Highlight outliers**: Use the slider to select ±σ residuals. Click “Select all ±σ outliers” to append them to the Accumulated code. Click “Clear ±σ outliers” to deselect and remove from the code box

11. **Copy all**: Click the Copy Icon to the right of the current or accumulated code box and paste into your own R script for documentation

12. **Apply Removals**: Click “Apply Removals” to remove each selected data points, from the current y-variable, to replace points with `NA` in a new .csv (raw data is unaffected), available using 'export cleaned data' and remove these values from view

13. **Reload original data**: Make a mistake or want a fresh start? Click Reload original data to reload the .csv from above to start over

14. **Export cleaned data**: Download the cleaned .csv reflecting your confirmed removals. This button will download a zip file containing your .csv, reflecting changes from using the "apply removals" button, and includes a compiled R script with the R code for those removals.

*Fluxtools is an independent project and is not affiliated with or endorsed by the AmeriFlux Network. “AmeriFlux” is a registered trademark of Lawrence Berkeley National Laboratory and is used here for identification purposes only.*
