-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbr40.Rmd
185 lines (122 loc) · 4.53 KB
/
mbr40.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
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)
```