-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
216 lines (166 loc) · 8.68 KB
/
README.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
## Setup ruODK
ruODK::ru_setup(
pid = 1,
fid = "pizza",
url = "https://odk.eha.io/",
un = Sys.getenv("ODK_CENTRAL_USERNAME"),
pw = Sys.getenv("ODK_CENTRAL_PASSWORD")
)
```
# formstools: Tools for working with ODK XLSForms
<!-- badges: start -->
[![R-CMD-check](https://github.com/ecohealthalliance/formstools/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ecohealthalliance/formstools/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
Set of utility functions for use by EcoHealth Alliance researchers in working with Open Data Kit XLSForms. These functions are aimed at aiding users in the data cleaning and data validation process using information found in the ODK XLSForms.
## Installation
You can install the development version of formstools like so:
``` r
if (!require(remotes)) install.packages("remotes")
remotes::install_github("ecohealthalliance/formstools)
```
## What does `formstools` do?
Currently, the `formstools` package can:
### Extract the questions for `select_one` and `select_multiple` types of questions in a given/specified XLSForm
The `get_questions()` function extracts the questions for all or for specified `select_one` and `select_multiple` types of questions. The function can be used as follows:
```{r example1}
## Load library
library(formstools)
## Extract all questions for all select_one and select_multiple types of questions
## in the example XLSForm ghana_community_form.xlsx
get_questions(
xlsform = system.file("extdata", "ghana_community_form.xlsx", package = "formstools")
)
```
The `get_questions` function can also extract questions for specific `select_one` or `select_multiple` types of questions if the user knows the `list_name` as shown below:
```{r example2}
## Extract all questions for select_one and/or select_multiple types of questions
## in the example XLSForm ghana_community_form.xlsx for the list_name gender
get_questions(
xlsform = system.file("extdata", "ghana_community_form.xlsx", package = "formstools"),
choice_name = "gender"
)
```
### Extract the choices for `select_one` and `select_multiple` types of questions in a given/specified XLSForm
The `get_choices()` function extracts the choices for all or for specified `select_one` and `select_multiple` types of questions. The function can be used as follows:
```{r example3}
## Extract all choices for all select_one and select_multiple types of questions
## in the example XLSForm ghana_community_form.xlsx
get_choices(
xlsform = system.file("extdata", "ghana_community_form.xlsx", package = "formstools")
)
```
The `get_choices` function can also extract choices for specific `select_one` or `select_multiple` types of questions if the user knows the `list_name` as shown below:
```{r example4}
## Extract all choices for select_one and/or select_multiple types of questions
## in the example XLSForm ghana_community_form.xlsx for the list_name gender
get_choices(
xlsform = system.file("extdata", "ghana_community_form.xlsx", package = "formstools"),
choice_name = "gender"
)
```
### Get choices for a specific form using a form schema extracted using `{ruODK}`
The form schema or codebook can be retrieved from ODK Central using `{ruODK}` as follows:
```{r ruODK_setup, eval = FALSE}
## Setup ruODK
ruODK::ru_setup(
pid = 1,
fid = "pizza",
url = "https://odk.eha.io/",
un = Sys.getenv("ODK_CENTRAL_USERNAME"),
pw = Sys.getenv("ODK_CENTRAL_PASSWORD")
)
```
```{r ruODK_schema}
## Retrieve form schema
pizza_schema <- ruODK::form_schema_ext()
pizza_schema
```
The function `get_choices_ruodk()` can be used to process this form schema to get the choices for `select_one` and `select_multiple` type of questions in a similar format as the result in the approach described in the previous functions. This function can be used as follows:
```{r process_schema}
get_choices_ruodk(form_schema = pizza_schema)
```
### Match `other` response/s to specified responses to a question in an ODK form
It is possible that enumerators enter an other response (usually in free text) to a question with specified and distinct responses (either single selection or multiple selection) that is the same or similar to the accepted responses. It would be desirable to recode these responses to correspond to the actual specified and distinct responses. The `match_other_to_choices()` performs this operation as follows:
```{r match_choices}
match_other_to_choices(
form_schema = pizza_schema,
form_data = ruODK::odata_submission_get(),
var_name = "pizza2",
other_var_name = "pizza3"
)
```
The output of using this function is a data.frame/tibble with rows equal to the number of rows as the form data and columns equal to the number of specified and distinct choices of the question/variable of interest plus identifying variable/s specified by the `id` argument. The columns for response values are named by each of the choices prefixed by the string `recode_`. Each column in this resulting data.frame provides values of either `1` or `0` with `1` meaning that that specific choice was detected from the other response for that specific row of data. This information can then be used in the data cleaning/processing steps to possibly add another response for the specific choice.
### Spread a vector of character responses/categorical values into a `data.frame`
Given a vector of categorical/character values, sometimes it is useful to be able to create a data.frame with columns for each of the unique values in the vector and rows equal to the length of the vector. The columns for each of the unique values would indicate whether that value was the response. For example:
```{r spread_data}
## Pet owned
pet <- c("dog", "dog", "dog", "cat", "cat", NA)
```
It might be useful to convert this vector into a data.frame with columns for each of the unique type of pet and with each row indicating which type of pet it is. The `spread_vector_to_columns()` function can be used for this as follows:
```{r spread_example}
spread_vector_to_columns(
x = pet,
fill = c("dog", "cat", "monkey"), ## All possible responses include monkey
prefix = "pet"
)
```
### Split ODK `select_multiple` type responses into a `data.frame`
Sometimes it is useful to split the ODK `select_multiple` type responses into a data.frame with columns for each of the response and rows equal to the length of the `select_multiple` type response vector. Using the pizza toppings example, we can use the `split_select_multiples()` function as follows:
```{r split_multiple_a}
split_multiple_responses(
x = pizza_data$pizza2,
fill = c("cheese", "tomatoes", "pepperoni", "mushrooms", "artichoke", "olives", "pineapple", "other"),
prefix = "toppings"
)
```
This can also be useful in splitting free text responses which are answers to questions that ask about other responses when not available from existing list of options. For example, in the pizza data, we can split the other responses for toppings as follows:
```{r split_multiple_b}
## Get all other responses provide by all respondents
all_other_choices <- stringr::str_split(pizza_data$pizza3, pattern = ",") |>
unlist() |>
stringr::str_trim() |>
tolower() |>
unique() |>
(\(x) x[!is.na(x)])()
## Apply split_multiple_responses
split_multiple_responses(
x = tolower(pizza_data$pizza3),
sep = ",",
fill = all_other_choices,
prefix = "other"
)
```
In the example above, we first create a vector (`all_other_choices`) of all unique *others* responses in the data and then put these responses in lower case. Then, we apply the split_multiple_responses to the others responses in the pizza data, we set the `sep` argument to a comma (`,`), use the `all_other_choices` vector for the `fill` argument, and use *other* as the value for the `prefix` argument. We then are able to create a tibble of *other* responses with each unique other response having its own column in the output data.frame. For respondents who report that specific response as *other*, then a value of 1 is recorded for that variable.
<!---
```{r cchf_setup}
## Setup ruODK
ruODK::ru_setup(
pid = 6,
fid = "Pilot_CCHF_participant_questionnaire",
url = "https://odk.eha.io/",
un = Sys.getenv("ODK_CENTRAL_USERNAME"),
pw = Sys.getenv("ODK_CENTRAL_PASSWORD")
)
```
```{r cchf_schema}
schema <- ruODK::form_schema_ext()
get_choices_ruodk(form_schema = schema, choice_name = "choices_english_(en)")
match_other_to_choices(
schema, ruODK::odata_submission_get(),
var_name = "p30_sick_care",
choice_name = "choices_english_(en)",
other_var_name = "p30_sick_care_other"
)
```
--->