---
title: "1.3) The preload() function"
author: "Hugo Flávio"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{1.3) The preload() function}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Index

1. [**Preparing your data**](a-0_workspace_requirements.html)
    1. [Structuring the study area](a-1_study_area.html)
    1. [Creating a distances matrix](a-2_distances_matrix.html)
    1. [**The preload() function**](a-3_preload.html)
1. [explore()](b-0_explore.html)
    1. [Processes behind explore()](b-1_explore_processes.html)
    1. [Inspecting the explore() results](b-2_explore_results.html)
1. [migration()](c-0_migration.html)
    1. [Processes behind migration()](c-1_migration_processes.html)
    1. [Inspecting the migration() results](c-2_migration_results.html)
    1. [One-way efficiency estimations](c-3_migration_efficiency.html)
1. [residency()](d-0_residency.html)
    1. [Processes behind residency()](d-1_residency_processes.html)
    1. [Inspecting the residency() results](d-2_residency_results.html)
    1. [Multi-way efficiency estimations](d-3_residency_efficiency.html)
1. [Manual mode](e-0_manual_mode.html)
1. [Beyond the three main analyses](f-0_post_functions.html)

## Note!

The content on this page assumes that 1) You are familiar with handling R objects and 2) you are familiar with the the traditional input files for actel. If you do not know which inputs are necessary to run actel, you can find more information about them [in the first page of the manual](a-0_workspace_requirements.html).

## The preload() function

If you are working with large databases, or retrieving your detection data from a server (such as ETN or OTN), it may be impractical to have to save all the data into CSV files to be able to run actel analyses. To help you out, I have created the function `preload()`, which pre-compiles R objects into a format that is actel-compatible.

Note:
  : The main functions will not accept a datapack that does not originate from a `preload()` call run during the same R session. This is to ensure that the data is correctly formatted and up to date.

To run the `preload()` function, you must first create four R objects, equal in structure to their CSV counterparts: biometrics, spatial, deployments and detections. Additionally, you also need to set the `tz` argument.

Once you have these four objects, you can run `preload()`:

```r
x <- preload(biometrics = biometrics, spatial = spatial, deployments = deployments, 
	detections = detections, tz = "Europe/Copenhagen")
```

You can then use the resulting object to run an actel analysis:

```r
results <- explore(datapack = x)
```

## What about the optional files?

If you want to use `preload()` and include a distances matrix or the equivalent of a 'spatial.txt' file, you must use the `distances` and `dot` arguments (respectively). I recommend that you calculate the distances matrix using the actel functions provided for that (more details [in this page](a-2_distances_matrix.html)). As for the dot argument, simply create a string that looks like what your 'spatial.txt' file would have looked like. e.g:


```r
# You can create multiple lines, just like in the spatial.txt case:
dot <- 
"River0 -- River1 -- River3 -- River4 -- River5 -- River6 -- Fjord1 -- Fjord2 -- Sea1
River1 -- River2 -- River3"


x <- preload(biometrics = biometrics, spatial = spatial, deployments = deployments, 
	detections = detections, distances = distances, dot = dot, tz = "Europe/Copenhagen")
```

## migration/residency-compatible datapacks

If your final goal is to run a `migration()` or `residency()` analysis, remember to have a 'Section' column in your spatial input. Apart from that, the process is exactly the same! Load your data using preload, and then source the loaded datapack into the analysis functions:

```r
results <- migration(datapack = x)

# or

results <- residency(datapack = x)
```

## Remaining arguments

The `preload()` function also contains the following arguments: `exclude.tags`, `disregard.parallels`, `discard.orphans`, `start.time` and `stop.time`. These perform exactly the same tasks as their counterparts on the main functions.

Note: 
  : If you are using preloaded data, these arguments will be disregarded in the main analysis call, so make sure to set them correctly in the `preload()` call!


## Final notes on the detections

If you load detections through `preload()`, they will be required to have four specific columns: Timestamp, Receiver, CodeSpace and Signal. You can use the functions `extractCodeSpaces()` and `extractSignals()` to help you out if needed. On top of these four columns, you can include two columns for sensor data: Sensor.Value and Sensor.Unit (notice the capital first letters). On top of these six columns, you can include as many columns as you want! These extra columns will be carried through the main analyses and exported in the `detections` and `valid.detections` objects.

Note:
  : Because, at this point, the detections are no longer influenced by the manufacturer, actel will not convert code-spaces. This means that whichever CodeSpace you selected for your tags, is the one that will be displayed at the end of the analyses!

**<a href="javascript:history.back()">Return to previous page</a>**

[Back to top.](#)

