---
title: "Manual trimming of density profiles"
author: "Luka Krajnc"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Manual trimming of density profiles}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Manual trimming
 Since package version 0.2 you can also trim the whole list of measurements by hand by using the function `trim_manually`. When using automatic trimming, it sometimes fails to detect the starting or ending point within the density profile. Failing to detect ending point is usually a consequence of the profile being bark-to-pith instead of bark-to-bark. In those cases (an others) you can trim the measurement by hand - either correcting failed automatic trimmings with ` correct_failures` or manually for the whole sample (`trim_manually`).

Load the library, a few density profiles and try to trim them automatically:
```{r setup}
library(densitr)
dp.list <- dpload(dp.directory = system.file("extdata", package = "densitr"))
dp.trimmed  <- dptriml(dp.list)
```

As you can see from the output above, the automatic trim detection of endings failed in 6 files, while no failures occurred in start detection.

In such cases, inspect the failed measurements by hand using `dptrim`, or by calling either `dpdetect_s` or `dpdetect_e` and using the argument `return.plot = TRUE`. You can also just plot it using `plot`.

```{r, fig.width=15, fig.height=5}
dptrim(dp.list[["00050045"]], return.plot = TRUE)
```

As seen from the plot, end detection failed due to the large increase in values towards the ending part of the profile.

In such cases or where you want to make a manual correction, you can use `manual_trim_detect` on an individual profile or `trim_manually` on list of density profiles. This will plot that profile and you can click on the plot to select the desired position. After selecting the point, your selection will be drawn on the plot. The function will also fail if your graphics device is something other than X11, windows or quartz.
```{r, eval = FALSE}
manual_trim_detect(dp.list[["00050045"]])
```

It is also possible to correct a whole set of trim failures sequentially by calling `correct_failures` on a list of trimmed profiles. In order for this to work, you have to call the `dptriml` or `dptriml_s` with the argument `rreport = TRUE`, which returns a list containing all trimmed profiles and a data frame with the trimming report. `correct_failures` will then separate the trim failures and run `manual_trim_detect` on all failures sequentially, asking you to pick a start or end for each failed profile. It will automatically subset the failed profiles and return a complete list of trimmed profiles, both those trimmed automatically and those trimmed manually.

```{r, eval = FALSE}
dp.trimmed  <- dptriml(dp.list, rreport = TRUE)
dp.new <- correct_failures(dp.trimmed)
```

You can also just remove the trim failures from the list of trimmed profiles by calling `remove_trim_failures` on that list, it will return only those where there were no failures. Failures can also be separated out by calling `separate_trim_failures` on a list of automatically trimmed profiles, which return two lists, one containing start failures and one containing end failures.
```{r}
dp.trimmed  <- dptriml(dp.list, rreport = TRUE)
dp.successful <- remove_trim_failures(dp.trimmed)
length(dp.successful)
dp.failed <- separate_trim_failures(dp.trimmed)
length(dp.failed$failures.end)
```

Trim failures can also be ignored in some cases. For example, if trimming both beginning and end, end trimming will sometimes fail due to the profile being bark-to-pith and not bark-to-bark. Plot the failures and inspect them before disregarding them, if no manual corrections are being done.
