-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkobo_ridl.R
290 lines (264 loc) · 14 KB
/
kobo_ridl.R
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# WARNING - Generated by {fusen} from dev/flat_dev.Rmd: do not edit by hand
#' Archive all crunching files in RIDL
#'
#' RIDL is UNHCR instance of a CKAN server and is accessible for UNHCR staff at
#' https://ridl.unhcr.org . It is designed to keep track and
#' document dataset within an organisation.
#'
#' You conveniently archive there your generated report and save
#' the work you did on a notebook: As you have been working on the data, you want
#' to keep track of it and save your work in a place where it can be useful for
#' other people and avaialble for peer review and quality assessment.
#'
#'
#' The function saves within the the RIDL container you used to get the data
#' from the following resources:
#' * the generated report
#' * the analysis plan, aka the extended xlsform used to record relabeling, clean, indicator creation, question grouping, exploration settings
#' * the source notebook
#'
#' The function behavior is the following -
#' 1. Get metadata from the RIDL dataset
#' 2. check if the resources to be uploaded is already shared based on the name
#' 3. if already there update, if not create
#'
#' The function relies on # install.packages("pak")
#' # pak::pkg_install("edouard-legoupil/riddle")
#'
#' @param ridl ridl container where the resources should be added
#' @param datafolder folder where the data used by the notebook are stored
#' @param form names of the file with the analysis plan
#' @param namethisfile all files are archived based on the name of notebook you created.
#' The function automatically get the name of the notebook where it is run from,
#' using
#' basename(rstudioapi::getSourceEditorContext()$path )
#'
#' @param visibility can be "public" per default or set to private for obscure reasons..
#' @param stage allow to document your analysis stage
#' * exploration_initial if your crunching is the very initial basic cleaning, relabeling of your data
#' * exploration_advance if your crunching
#' * interpretation_prez
#' * dissemination_story
#'
#' @importFrom riddle dataset_show resource_metadata resource_create resource_update
#'
#' @return nothing all analysis files are added as a resources
#'
#' @export
#' @examples
#'
#' ### Example used for each template
#' ## Time to archive your work once done!!
#' # namethisfile = basename(rstudioapi::getSourceEditorContext()$path )
#' # if( params$publish == "yes"){
#' # kobo_ridl(ridl = params$ridl,
#' # datafolder = params$datafolder,
#' # form = params$form,
#' # namethisfile = namethisfile ,
#' # visibility = params$visibility,
#' # stage = params$stage) }
#'
#'
# prefixer::import_from(fun = kobo_ridl)
kobo_ridl <- function(ridl,
datafolder,
form,
namethisfile,
visibility = "public",
stage = "explo_initial"){
### name of the file...
# retrieving path from getSourceEditorContext()
# using $ operator
## Enter below the name you used to save this file - without extension
# namethisfile <- rstudioapi::getSourceEditorContext()$path
### Remove file name extension
name <- substr(namethisfile,1, nchar(namethisfile) -4)
#
# ## Now just the name of the file
# name1 <- basename(rstudioapi::getSourceEditorContext()$path )
# ## Remove file name extension
# name <- substr(name1,1, nchar(name1) -4)
#
# ### Time to archive your work once done!!
time <- format(Sys.Date(), '%d%b%y')
# ### Uncomment and Run this only once you have knitted your file in order to quickly upload this to RIDL -
# install.packages("pak")
# pak::pkg_install("edouard-legoupil/riddle")
# First let's get the dataset metadata
p <- riddle::dataset_show(ridl)
list_of_resources <- p[["resources"]][[1]]
#names(list_of_resources)
# "description"
# "file_type" questionnaire microdata report script
# "format"
# "id" "name"
# "type" "url"
# "visibility"
# "identifiability"
# "version"
# "script_dependencies"
# "script_instructions"
# "script_software"
# "source_code_repo"
### 1. Publish the analysis plan #######
nameanalysisplan = paste0("analysisPlan_", stage,"_", name,"_", ridl,"_", time )
### Check if the name is already in the resources
if(nameanalysisplan %in% list_of_resources$name) {
## get the resource id
resourceid <- list_of_resources |>
dplyr::filter ( name == nameanalysisplan) |>
dplyr::pull(id)
## get the new resource version
curversion <- list_of_resources |>
dplyr::filter ( name == nameanalysisplan) |>
dplyr::pull(version)
## Build resource metadata
metadataanalysisplan <- riddle::resource_metadata(
type = "script",
url = paste0("analysisPlan_", stage,"_", name, "_", ridl,"_", time, ".xlsx"),
name = nameanalysisplan,
description = paste0("Analysis Plan to use with: ", stage,"_", name,"_", ridl,"_", time,
". Built using kobocruncher "),
format = ".xlsx",
version = (curversion + 1),
visibility = visibility,
# file_type = "other",
file_type = "script",
script_dependencies= "kobocruncher",
script_instructions= "This excel file contains all specific metedata
to relabel the original data, to group variable for automatic data
exploration and to create calculated variables ",
script_software= "R",
source_code_repo= "https://edouard-legoupil.github.io/kobocruncher",
## Revise here based on the name from your crunching report
upload = httr::upload_file(here::here(datafolder, form))
)
riddle::resource_update(id = resourceid,
res_metadata = metadataanalysisplan)
} else {
metadataanalysisplan <- riddle::resource_metadata(
type = "script",
url = paste0("analysisPlan_", stage,"_", name, "_", ridl,"_", time, ".xlsx"),
name = nameanalysisplan,
description = paste0("Analysis Plan to use with: ", stage,"_", name,"_", ridl,"_", time,
". Built using kobocruncher "),
format = ".xlsx",
visibility = visibility,
# file_type = "other",
file_type = "script",
script_dependencies= "kobocruncher",
script_instructions= "This excel file contains all specific metedata
to relabel the original data, to group variable for automatic data
exploration and to create calculated variables ",
script_software= "R",
source_code_repo= "https://edouard-legoupil.github.io/kobocruncher",
## Revise here based on the name from your crunching report
upload = httr::upload_file(here::here(datafolder, form))
)
riddle::resource_create(package_id = p$id,
res_metadata = metadataanalysisplan)
}
### 2. Now publish the current notebook #######
namenotebook = paste0("notebook_", stage, name, ridl, time)
### Check if the name is already in the resources
if(namenotebook %in% list_of_resources$name) {
## get the resource id
resourceid <- list_of_resources |>
dplyr::filter ( name == nameanalysisplan) |>
dplyr::pull(id)
## get the new resource version
curversion <- list_of_resources |>
dplyr::filter ( name == nameanalysisplan) |>
dplyr::pull(version)
metadataanalysisscript <- riddle::resource_metadata(type = "script",
url = paste0("notebook_", stage, name, ridl, time, ".Rmd"),
name = namenotebook,
description = paste0("Notebook for ",
stage,"_", name,"_", ridl,"_", time,
" report. Built using kobocruncher "),
format = "rmd",
version = (curversion + 1),
visibility = visibility,
# file_type = "other",
file_type = "script",
script_dependencies= "kobocruncher",
script_instructions= "Use the data and the analysis
plan documented in the report parameters and
available within this RIDL dataset ",
script_software= "R",
source_code_repo= "https://edouard-legoupil.github.io/kobocruncher",
# Revise here based on the name from your crunching report
upload = httr::upload_file(here::here(namethisfile))
)
riddle::resource_update(id = resourceid,
res_metadata = metadataanalysisscript)
} else {
metadataanalysisscript <- riddle::resource_metadata(type = "script",
url = paste0("notebook_", stage, name, ridl, time, ".Rmd"),
name = namenotebook,
description = paste0("Notebook for ",
stage,"_", name,"_", ridl,"_", time,
" report. Built using kobocruncher "),
format = "Rmd",
visibility = visibility,
# file_type = "other",
file_type = "script",
script_dependencies= "kobocruncher",
script_instructions= "Use the data and the analysis
plan documented in the report parameters and
available within this RIDL dataset ",
script_software= "R",
source_code_repo= "https://edouard-legoupil.github.io/kobocruncher",
## Revise here based on the name from your crunching report
upload = httr::upload_file(here::here(namethisfile))
)
riddle::resource_create(package_id = p$id,
res_metadata = metadataanalysisscript)
}
## 3. and now the generated report - that should be hopefully already generated ####
namereport = paste0("output_", stage, name, ridl, time)
if(stage == "interpretation_prez") { filext <- ".pptx"} else if(stage == "technical_report") { filext <- ".docx" } else { filext <- ".html" }
### Check if the name is already in the resources
if(namereport %in% list_of_resources$name) {
## get the resource id
resourceid <- list_of_resources |>
dplyr::filter ( name == namereport) |>
dplyr::pull(id)
## get the new resource version
curversion <- list_of_resources |>
dplyr::filter ( name == namereport) |>
dplyr::pull(version)
metadatareport <- riddle::resource_metadata(type = "attachment",
url = paste0(stage, name, ridl, time,".html"),
name = paste0(stage, name, ridl, time),
description = paste0("Generated Report: ",
stage,", ", name, " built on ", time,
" using kobocruncher "),
format = filext,
version = (curversion + 1),
visibility = visibility,
file_type = "report",
## Revise here based on the name from your crunching report
upload = httr::upload_file(here::here(paste0(name,filext)))
)
riddle::resource_update(id = resourceid,
res_metadata = metadatareport)
} else {
metadatareport <- riddle::resource_metadata(type = "attachment",
url = paste0(stage, name, ridl, time,".html"),
name = paste0(stage, name, ridl, time),
description = paste0("Generated Report: ",
stage,", ", name, " built on ", time,
" using kobocruncher "),
format = filext,
visibility = visibility,
file_type = "report",
## Revise here based on the name from your crunching report
upload = httr::upload_file(here::here(paste0(name,filext)))
)
riddle::resource_create(package_id = p$id,
res_metadata = metadatareport)
}
## Once all of it done, let's the user know about it...
return( cat(paste0("Congrat! The Notebook", name, " together with linked output and analysis plan have been published on RIDL")))
}