---
title: "Pilot: MBR 4.0"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pilot: MBR 4.0}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
url_defined <- Sys.getenv("MBR40_URL") != ""
```

## Install R package


```{r install, eval = FALSE}

# Install latest stable version from KWB-R R-Universe
options(repos = c(
    kwbr = 'https://kwb-r.r-universe.dev',
    CRAN = 'https://cloud.r-project.org'))


install.packages('kwb.pilot')


```

## Setup Environment Variables 

For downloading the `latest` (from `Martin Systems webportal`) and `archived` (from 
`KWB Nextcloud`) `.tsv` data files  you need set the following environment variables  
**only once** as shown below. 

For doing so follow the steps below: 

1. Open `RStudio` and run `usethis::edit_r_environ()`

2. In the opened window add the required environment variables

```{r r_environment, eval = FALSE}
MBR40_URL = "url-to-download-pilot-plant-data"
NEXTCLOUD_URL = "https://<replace-with-dwc-cloud-url>"
NEXTCLOUD_USER = "<your-dwc-cloud-username>" # your username
NEXTCLOUD_PASSWORD = "your-nextcloud-app-password" ### see details below
```

To access the `archived` data you need to be a registered Nextcloud user with 
access to the folder `projects/MBR4.0`

For creating `<your-nextcloud-app-password>`:

* go to: https://replace-with-kwb-cloud-url/index.php/settings/user/security

* scroll down to `create new app password`

* select a name e.g. `r-script` and copy the token and replace `your-nextcloud-app-password`


3. Finally you need to restart Rstudio and proceed with the code below:


Subsequently click **Save** and restart RStudio.

## Run Shiny App 

For running the shiny app it is needed to setup the environment variables as shown above and run the following
code.

### Latest Data

To use the `latest data` (downloads data from web) or if you run the app for the first time you need to run: 

```{r app_latest, eval=FALSE}
kwb.pilot::run_app("mbr4.0", use_live_data = TRUE)
```

### Local Data

Once you have done the step above for one time, you can also just interactively load you datasets, which you downloaded
during the command above by running:

```{r app_archive, eval=FALSE}
kwb.pilot::run_app("mbr4.0", use_live_data = FALSE)
```

## Define paths

For performing data aggregation and export to post-process the data in `Excel` in a `wide` format the workflow defined 
below has to be followed.

### Option 1: At KWB

In `paths_list` you need to replace the dummy value `kwb-server` 
for the key `servername`.

```{r paths_kwb, eval = FALSE}

#KWB server
paths_list <- list(
  servername = "kwb-server",
  root = "//<servername>/projekte$/WWT_Department/Projects/MBR 4.0/Exchange/",
  rawdata = "<root>/20_rawdata",
  processing = "<root>/21_processing",
  online =  "<rawdata>/online_data",
  export_dir = "<processing>/online-data"
  )

if(dir.exists(paths$online)) {
paths <- kwb.utils::resolve(paths_list)
}
```

### Option 2: At Home

```{r paths_home, eval = FALSE}
paths_list <- list(
  root = "C:/kwb/projects/mbr4.0",
  rawdata = "<root>/20_rawdata",
  processing = "<root>/21_processing",
  online =  "<rawdata>/online_data",
  export_dir = "<processing>/online-data"
)

# Only execute "At Home" if not at "KWB" (i.e. KWB server path does not exist) 
fs::dir_create(paths$online, recurse = TRUE)
paths <- kwb.utils::resolve(paths_list)

```


## Import 

```{r import, eval = url_defined}
library(kwb.pilot)

mbr4_data_raw <- kwb.pilot::read_mbr4()
str(mbr4_data_raw)

mbr4_data_tidy_raw <- kwb.pilot::tidy_mbr4_data(mbr4_data = mbr4_data_raw) 

str(mbr4_data_tidy_raw)

```

## Aggregate

```{r aggregate, eval = url_defined}
mbr4_data_tidy_10min <- mbr4_data_tidy_raw %>%   
  kwb.pilot::group_datetime(by = 600) 

mbr4_data_tidy_hour <- mbr4_data_tidy_10min %>%
  kwb.pilot::group_datetime(by = 3600)
```

## Export 

### CSV files 

Raw and aggregated data will be exported as `csv` in `wide` format (by running `kwb.pilot::long_to_wide()`) as shown below:

```{r export_data, eval = FALSE}

kwb.pilot::export_data(df_long = mbr4_data_tidy_raw,
                       export_dir = paths$export_dir)

kwb.pilot::export_data(df_long = mbr4_data_tidy_list_10min,
                       export_dir = paths$export_dir)

kwb.pilot::export_data(df_long = mbr4_data_tidy_list_hour,
                       export_dir = paths$export_dir)
```

Finally you can check the datasets in the export folder 
by running:


```{r export_data_show, eval = FALSE}
kwb.utils::hsOpenWindowsExplorer(paths$export_dir)
```
