-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultimate.Rmd
193 lines (154 loc) · 6.31 KB
/
ultimate.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
186
187
188
189
190
191
192
---
title: "Pilot: Ultimate"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Pilot: Ultimate}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
credentials_defined <- kwb.pilot::check_env_influxdb_ultimate() && kwb.pilot::check_env_nextcloud()
is_github <- identical(Sys.getenv("CI"), "true")
#eval_chunks <- credentials_defined && is_github
eval_chunks = FALSE
```
## Define Login Credentials
For writing to InfluxDBCloud you need to run `usethis::edit_r_environ()` and
define at least the following environment variables:
```{r echo = TRUE, eval = FALSE}
###############################################################################
### For InfluxDB-Cloud
###############################################################################
ULTIMATE_INFLUXDB_URL = "<ultimate_influxdbcloud_url>"
# InfluxDB API-Token with Write for bucket "ultimate"
ULTIMATE_INFLUXDB_TOKEN = "<ultimate_influxdbcloud_apitoken>"
ULTIMATE_INFLUXDB_ORG = "<ultimate_influxdbcloud_organisation>"
################################################################################
## Optional (for downloading raw data from Nextcloud)
################################################################################
NEXTCLOUD_URL = "https://<replace-with-nextcloud-cloud-url>"
NEXTCLOUD_USER = "<your-nextcloud-username>" # your username
NEXTCLOUD_PASSWORD = "your-nextcloud-app-password" ### see details below
#3 For creating <your-nextcloud-app-password>:
#3.1 go to: https://replace-with-nextcloud-url/index.php/settings/user/security
#3.2 scroll down to create new app password
#3.3 select a name e.g. r-script and copy the token and replace your-nextcloud-app-password
```
Finally you need to restart Rstudio and proceed with the code below:
## Install R package
```{r install, echo = TRUE, eval = FALSE}
# Enable repository from kwb-r
options(repos = c(
kwbr = 'https://kwb-r.r-universe.dev',
CRAN = 'https://cloud.r-project.org'))
# Download and install kwb.pilot in R
install.packages('kwb.pilot')
```
## Define Paths
```{r define_paths, eval = TRUE}
library(kwb.pilot)
paths_list <- list(
root_ci = tempdir(),
root_windows = "C:/kwb",
root = "",
common_root = "projects/ultimate/raw_data_pilots",
data_raw = "data",
data_imp = "data_imported",
a_site_code = "Pilot_A",
b_site_code = "Pilot_B",
a_cloud = "<common_root>/<a_site_code>",
a_cloud_raw = "<a_cloud>/<data_raw>",
a_cloud_imported = "<a_cloud>/<data_imp>",
a_local = "<root>/<a_cloud>",
a_local_raw = "<a_local>/<data_raw>",
a_local_imported = "<a_local>/<data_imp>",
b_cloud = "<common_root>/<b_site_code>",
b_cloud_raw = "<b_cloud>/<data_raw>",
b_cloud_imported = "<b_cloud>/<data_imp>",
b_local = "<root>/<b_cloud>",
b_local_raw = "<b_local>/<data_raw>",
b_local_imported = "<b_local>/<data_imp>"
)
paths <- kwb.utils::resolve(paths_list,
root = ifelse(identical(Sys.getenv("CI"),
"true"),
paths_list$root_ci,
paths_list$root_windows)
)
```
### Write to InfluxDBCloud
```{r a_download_import_move, eval = eval_chunks}
################################################################################
### 1. Write Pilot Plant Raw Data to InfluxDB Cloud
################################################################################
################################################################################
### 1.1 Pilot A
################################################################################
## Step: kwb.pilot::download_nextcloud_files() is optional and can be skipped
## if the data is provided in the `paths$raw_data_dir`
files_pilot_a <- kwb.pilot::download_nextcloud_files(dir_cloud = paths$a_cloud_raw,
dir_local = paths$a_local_raw)
if(length(files_pilot_a) > 0) {
tsv_paths <- list.files(
path = paths$a_local_raw,
full.names = TRUE,
pattern = "xls$"
)
kwb.pilot::write_to_influxdb_loop(
tsv_paths = tsv_paths,
paths = list(site_code = paths$a_site_code,
raw_data_dir = paths$a_local_raw),
changed_only = FALSE,
max_tsv_files = 10,
batch_size = 5000
)
### Move all files for Pilot A`s cloud "raw" data directory to the "imported"
### directory (in case that a file is already existing there: overwrite it)!
paths_pilot_a <- data.frame(raw = file.path(paths$a_cloud_raw, files_pilot_a),
imported = file.path(paths$a_cloud_imported, files_pilot_a)
)
kwb.pilot::move_nextcloud_files(paths_pilot_a, overwrite = TRUE)
} else {
message(sprintf("Nothing to do for '%s'! No new files in '%s'",
paths$a_site_code,
paths$a_cloud_raw))
}
```
```{r b_download_import_move, eval = eval_chunks}
################################################################################
### 1.2 Pilot B
################################################################################
## Step: kwb.pilot::download_nextcloud_files() is optional and can be skipped
## if the data is provided in the `paths$raw_data_dir`
files_pilot_b <- kwb.pilot::download_nextcloud_files(dir_cloud = paths$b_cloud_raw,
dir_local = paths$b_local_raw)
if(length(files_pilot_b) > 0) {
tsv_paths <- list.files(
path = paths$b_local_raw,
full.names = TRUE,
pattern = "xls$"
)
kwb.pilot::write_to_influxdb_loop(
tsv_paths = tsv_paths,
paths = list(site_code = paths$b_site_code,
raw_data_dir = paths$b_local_raw),
changed_only = FALSE,
max_tsv_files = 5,
batch_size = 5000
)
### Move all files for Pilot B`s cloud "raw" data directory to the "imported"
### directory (in case that a file is already existing there: overwrite it)!
paths_pilot_b <- data.frame(raw = file.path(paths$b_cloud_raw, files_pilot_b),
imported = file.path(paths$b_cloud_imported, files_pilot_b)
)
kwb.pilot::move_nextcloud_files(paths_pilot_b, overwrite = TRUE)
} else {
message(sprintf("Nothing to do for '%s'! No new files in '%s'",
paths$b_site_code,
paths$b_cloud_raw))
}
```