---
title: "01a Data import"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{01a Data import}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Introduction

The aim of this document is to outline the basic workflow of importing data downloaded from the [ICES Regional Database & Estimation System (RDBES)](https://rdbes.ices.dk/#/) or a `list` object containing data frames (or data.tables) into `R` using the `RDBEScore` package.

The function **createRDBESDataObject** is intended to directly import Commercial Landing (CL), Commercial Effort (CE) and Commercial Sampling (CS) tables downloaded from [RDBES](https://rdbes.ices.dk/#/).

## Load the package

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

```{r}
#the directory holding the data to import
dataDir <- "./yourDataDirectory/"

#for demonstration purposes we will use the example data included in the package
dataDir <- paste0(system.file("extdata", package = "RDBEScore"), "/")
```


## Importing zipped files

It can directly import the `.zip` archive from the RDBES download containing all mandatory hierarchy tables plus VD and SL:

```{r}
importedH1 <- createRDBESDataObject(input = paste0(dataDir,"H1_Example.zip"))
#print the not NULL table names
names(importedH1[!unlist(lapply(importedH1, is.null))])
```
The easiest way to get a glimpse of the imported data hierarchy and single table row counts is just to print it. The information also includes the range of number sampled and number total for each table together with the selection method and number of rows.
```{r}
#calls the print function 
importedH1
```


It can import the CL, CE, VD or SL tables `.zip` archives, but will include all other tables as `NULL`:

```{r}
importedSL <- createRDBESDataObject(input = paste0(dataDir,"HSL_Example.zip"))
#print the not NULL table names
importedSL
```

It can also handle overwriting `zip` file original files with files appearing later in the list. However each overwrite results in a warning!

```{r}
importFiles <- c(paste0(dataDir,"HSL_Example.zip"),
                 paste0(dataDir,"H1_Example.zip"))
importedTables <- createRDBESDataObject(input = importFiles)
#print the not NULL table names
names(importedTables[!unlist(lapply(importedTables, is.null))])
```

## Importing csv files

It can also import the unzipped `.csv` files with the default RDBES names:

```{r}
importedVS <- createRDBESDataObject(input = dataDir,
                                    listOfFileNames = list("VS" = "VesselSelection.csv"))
#print the not NULL table names
names(importedVS[!unlist(lapply(importedVS, is.null))])
```

## Importing list of data frames

It can also import a `list` object containing data frames (or data.tables). However, it should be noted that this type of import bypasses the RDBES upload data integrity checks.

```{r}
#list of data frames 
listOfDfsH1 <- readRDS(paste0(dataDir,"H1_Example.rds"))
#print the class of the list elements 
sapply(listOfDfsH1, class)
```

```{r}
importedList <- createRDBESDataObject(listOfDfsH1)
```

## Object class RDBESDataObject

It should be noted that the objects created are of the S3 class "**RDBESDataObject**". The class has defined **print()**, **summary()** and **sort()** methods. For more info on theese see vignette [Manipulating RDBESDataObjects](manipulating-rdbesdataobjects.html). 

```{r}
importedTables <- createRDBESDataObject(paste0(dataDir,"H1_Example.zip"))
class(importedTables)
```


### validate RDBESDataObject

RDBESDataObject structure can be validated using the **validateRDBESDataObject()** function.

```{r}
validateRDBESDataObject(importedTables, verbose = TRUE)
```


To see what you can do with the imported **RDBESDataObject** see other vignettes like [01b Manipulating RDBESDataObjects](01b-manipulating-rdbesdataobjects.html).

See also other package vignettes:

```{r,echo=F, results='asis', eval=T}
# note: vignettes are first compiled and only then html, R and Rmd are moved to folder doc
vignettes <- data.frame(Rmd=dir()[grepl(dir(), pat=".Rmd")])
# determines index of vignette being compiled
thisVignetteRmd<-paste0(gsub(":","",gsub(" ", "-",as.character(rmarkdown::metadata$title))),".Rmd")
# prints list with links
for (i in seq_along(vignettes$Rmd)) {
  if(tolower(vignettes$Rmd[i])!=tolower(thisVignetteRmd)){
  cat(sprintf("- [%s](%s)\n", gsub("-"," ",gsub(".Rmd","",vignettes$Rmd[i])), gsub(".Rmd",".html",vignettes$Rmd[i])))
}
}
```

#END

