---
title: "Scan Examples"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
  html_document:
    code_folding: show
    df_print: paged
    number_sections: yes
    toc: yes
    toc_depth: 3
    toc_float: yes
editor_options:
  chunk_output_type: console
vignette: >
  %\VignetteIndexEntry{Scan Examples}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r setup, include = FALSE}
# global knitting options for code rendering
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>")
```

# Introduction

Isoreader supports several dual inlet IRMS data formats. This vignette shows some of the functionality for scan data files. For additional information on operations more generally (caching, combining read files, data export, etc.), please consult the [operations vignette](https://isoreader.isoverse.org/articles/operations.html). For details on downstream data processing and visualization, see the [isoprocessor package](https://isoprocessor.isoverse.org).

Note: this vignette is still a work in progress.


```{r, message=FALSE}
# load isoreader package
library(isoreader)
```


# Reading files

Reading scan files is as simple as passing one or multiple file or folder paths to the `iso_read_scan()` function. If folders are provided, any files that have a recognized scan file extensions within those folders will be processed (e.g. all `.scn`). Here we read several files that are bundled with the package as examples (and whose paths can be retrieved using the `iso_get_reader_example()` function).

```{r, message=FALSE}
# all available examples
iso_get_reader_examples() %>% knitr::kable()
```

```{r}
# read scan examples
scan_files <-
  iso_read_scan(
    iso_get_reader_example("peak_shape_scan_example.scn"),
    iso_get_reader_example("background_scan_example.scn"),
    iso_get_reader_example("full_scan_example.scn"),
    iso_get_reader_example("time_scan_example.scn")
  )
```

# File summary

The `scan_files` variable now contains a set of isoreader objects, one for each file. Take a look at what information was retrieved from the files using the `iso_get_data_summary()` function.

```{r}
scan_files %>% iso_get_data_summary() %>% knitr::kable()
```

## Problems

In case there was any trouble with reading any of the files, the following functions provide an overview summary as well as details of all errors and warnings, respectively. The examples here contain no errors but if you run into any unexpected file read problems, please file a bug report in the [isoreader issue tracker](https://github.com/isoverse/isoreader/issues).

```{r}
scan_files %>% iso_get_problems_summary() %>% knitr::kable()
scan_files %>% iso_get_problems() %>% knitr::kable()
```

# File Information

Detailed file information can be aggregated for all isofiles using the `iso_get_file_info()` function which supports the full [select syntax](https://dplyr.tidyverse.org/reference/select.html) of the [dplyr](https://dplyr.tidyverse.org/) package to specify which columns are of interest (by default, all file information is retrieved).

```{r}
# all file information
scan_files %>% iso_get_file_info(select = c(-file_root)) %>% knitr::kable()
```

## Select/Rename

File information can also be modified across an entire collection of isofiles using the `iso_select_file_info()` and `iso_rename_file_info()` functions:

```{r}
# select + rename specific file info columns
scan_files2 <- scan_files %>%
  iso_select_file_info(-file_root) %>%
  iso_rename_file_info(`Date & Time` = file_datetime)

# fetch all file info
scan_files2 %>% iso_get_file_info() %>% knitr::kable()
```


## Filter

Any collection of isofiles can also be filtered based on the available file information using the function `iso_filter_files`. This function can operate on any column available in the file information and supports full [dplyr](https://dplyr.tidyverse.org/reference/filter.html) syntax.

```{r}
# find files that have 'CIT' in the new ID field
scan_files2 %>%
  iso_filter_files(type == "High Voltage") %>%
  iso_get_file_info() %>%
  knitr::kable()
```

## Mutate

The file information in any collection of isofiles can also be mutated using the function `iso_mutate_file_info`. This function can introduce new columns and operate on any existing columns available in the file information (even if it does not exist in all files) and supports full [dplyr](https://dplyr.tidyverse.org/reference/mutate.html) syntax.

```{r}
scan_files3 <- scan_files2 %>%
  iso_mutate_file_info(
    # introduce new column
    `Run in 2019?` = `Date & Time` > "2019-01-01" & `Date & Time` < "2020-01-01"
  )

scan_files3 %>%
  iso_get_file_info() %>%
  knitr::kable()
```

# Resistors

Additionally, some IRMS data files contain resistor information that are useful for downstream calculations (see e.g. section on signal conversion later in this vignette):

```{r}
scan_files %>% iso_get_resistors() %>% knitr::kable()
```

# Raw Data

The raw data read from the scan files can be retrieved similarly using the `iso_get_raw_data()` function. Most data aggregation functions also allow for inclusion of file information using the `include_file_info` parameter, which functions identically to the `select` parameter of the `iso_get_file_info` function discussed earlier.

```{r}
# get raw data with default selections (all raw data, no additional file info)
scan_files %>% iso_get_raw_data() %>% head(n=10) %>% knitr::kable()
# get specific raw data and add some file information
scan_files %>%
  iso_get_raw_data(
    # select just time and the two ions
    select = c(x, x_units, v44.mV, v45.mV),
    # include the scan type and rename the column
    include_file_info = c(`Scan Type` = type)
  ) %>%
  # look at first few records only
  head(n=10) %>% knitr::kable()
```

# For expert users: retrieving all data

For users familiar with the nested data frames from the [tidyverse](https://www.tidyverse.org/) (particularly [tidyr](https://tidyr.tidyverse.org/)'s `nest` and `unnest`), there is an easy way to retrieve all data from the iso file objects in a single nested data frame:

```{r}
all_data <- scan_files %>% iso_get_all_data()
# not printed out because this data frame is very big
```


# Saving collections

Saving entire collections of isofiles for retrieval at a later point is easily done using the `iso_save` function which stores collections or individual isoreader file objects in the efficient R data storage format `.rds` (if not specified, the extension `.scan.rds` will be automatically appended). These saved collections can be conveniently read back using the same `iso_read_scan` command used for raw data files.

```{r}
# export to R data archive
scan_files %>% iso_save("scan_files_export.scan.rds")

# read back the exported R data storage
iso_read_scan("scan_files_export.scan.rds")
```

# Data Export

At the moment, isoreader supports export of all data to Excel and the [Feather file format](https://blog.rstudio.com/2016/03/29/feather/) (a Python/R cross-over format). Note that both export methods have similar syntax and append the appropriate file extension for each type of export file (`.scan.xlsx` and `.scan.feather`, respectively).

```{r}
# export to excel
scan_files %>% iso_export_to_excel("scan_files_export")

# data sheets available in the exported data file:
readxl::excel_sheets("scan_files_export.scan.xlsx")
```

```{r}
# export to feather
scan_files %>% iso_export_to_feather("scan_files_export")

# exported feather files
list.files(pattern = ".scan.feather")
```
