| Title: | Interact with Data on 'SurveyCTO' |
|---|---|
| Description: | 'SurveyCTO' is a platform for mobile data collection in offline settings. The 'rsurveycto' R package uses the 'SurveyCTO' REST API <https://docs.surveycto.com/05-exporting-and-publishing-data/05-api-access/01.api-access.html> to read datasets and forms from a 'SurveyCTO' server into R as 'data.table's and to download file attachments. The package also has limited support to write datasets to a server. |
| Authors: | Jake Hughey [aut, cre], Robert On [aut] |
| Maintainer: | Jake Hughey <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.6 |
| Built: | 2026-05-09 08:31:48 UTC |
| Source: | https://github.com/agency-fund/rsurveycto |
An empty column is one whose only values are NA or "".
drop_empties(d)drop_empties(d)
d |
|
d modified by reference, invisibly.
library('data.table') d = data.table(w = 3:4, x = c('', 'foo'), y = c(NA, NA), z = c(NA, '')) drop_empties(d)library('data.table') d = data.table(w = 3:4, x = c('', 'foo'), y = c(NA, NA), z = c(NA, '')) drop_empties(d)
SurveyCTO's API supports basic authentication using a username and password. Make sure the user is assigned a role with permission to download data ("data manager" or greater) and "Allow server API access" is enabled.
scto_auth( auth_file = NULL, servername = NULL, username = NULL, password = NULL, validate = TRUE )scto_auth( auth_file = NULL, servername = NULL, username = NULL, password = NULL, validate = TRUE )
auth_file |
String indicating path to a text file containing the
server name on the first line, username on the second, and password on the
third. Other arguments are only used if |
servername |
String indicating name of the SurveyCTO server. |
username |
String indicating username for the SurveyCTO account. |
password |
String indicating password for the SurveyCTO account. |
validate |
Logical indicating whether to validate credentials by calling
|
scto_auth object for an authenticated SurveyCTO session.
## Not run: # preferred approach auth = scto_auth('scto_auth.txt') # alternate approach auth = scto_auth('my_server', 'my_user', 'my_pw', auth_file = NULL) ## End(Not run)## Not run: # preferred approach auth = scto_auth('scto_auth.txt') # alternate approach auth = scto_auth('my_server', 'my_user', 'my_pw', auth_file = NULL) ## End(Not run)
This function can download encrypted and unencrypted files attached to forms.
scto_get_attachments( auth, urls, output_dir, private_key = NULL, overwrite = TRUE )scto_get_attachments( auth, urls, output_dir, private_key = NULL, overwrite = TRUE )
auth |
|
urls |
Character vector of API URLs for file attachments. Will typically
be derived from a column of a |
output_dir |
String indicating path to directory in which to save files. |
private_key |
String indicating path to private key file. Only needs to
be non- |
overwrite |
Logical indicating whether to overwrite existing files. |
A character vector of file names of the same length as urls, with
NA for missing or invalid URLs.
## Not run: auth = scto_auth('scto_auth.txt') form_data = scto_read(auth, 'my_form', 'form') filenames = scto_get_attachments(auth, form_data[['my_attachment']], '.') ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') form_data = scto_read(auth, 'my_form', 'form') filenames = scto_get_attachments(auth, form_data[['my_attachment']], '.') ## End(Not run)
This function fetches definitions for currently deployed forms. It has been
superseded in favor of scto_get_form_metadata(), which fetches metadata,
including defintions, for deployed and previous versions of forms.
scto_get_form_definitions(auth, form_ids = NULL, simplify = TRUE)scto_get_form_definitions(auth, form_ids = NULL, simplify = TRUE)
auth |
|
form_ids |
Character vector indicating the form ids. |
simplify |
Logical indicating whether to return the definition for one form as a simple list instead of a named, nested list. |
If simplify is TRUE and getting one form definition, a list.
Otherwise a named list of lists containing the definition for each form.
## Not run: auth = scto_auth('scto_auth.txt') form_def = scto_get_form_definitions(auth, 'my_form') form_defs = scto_get_form_definitions(auth) ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') form_def = scto_get_form_definitions(auth, 'my_form') form_defs = scto_get_form_definitions(auth) ## End(Not run)
This function fetches metadata, including form definitions, for deployed and previous versions of one or more forms.
scto_get_form_metadata( auth, form_ids = NULL, deployed_only = FALSE, get_defs = TRUE, def_dir = NULL )scto_get_form_metadata( auth, form_ids = NULL, deployed_only = FALSE, get_defs = TRUE, def_dir = NULL )
auth |
|
form_ids |
Character vector indicating the form ids. |
deployed_only |
Logical indicating whether to fetch metadata for all versions of each form, or only for the deployed version. |
get_defs |
Logical indicating whether to fetch form definitions. |
def_dir |
String indicating directory in which to save the form definitions as Excel files. |
A data.table with one row per form (and per version, if
deployed_only is FALSE). Definitions are returned as nested
data.tables, which can be unnested using
scto_unnest_form_definitions().
## Not run: auth = scto_auth('scto_auth.txt') form_metadata = scto_get_form_metadata(auth, 'my_form') form_defs = scto_unnest_form_definitions(form_metadata) ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') form_metadata = scto_get_form_metadata(auth, 'my_form') form_defs = scto_unnest_form_definitions(form_metadata) ## End(Not run)
This function converts data from scto_read() to long format, then joins
the data with the "survey" and "choices" components of the form metadata.
scto_get_form_responses( auth, form_id, id_cols = c("KEY", "formdef_version", "CompletionDate", "SubmissionDate", "starttime", "endtime", "review_status", "review_quality"), exclude_cols = "instanceID", ... )scto_get_form_responses( auth, form_id, id_cols = c("KEY", "formdef_version", "CompletionDate", "SubmissionDate", "starttime", "endtime", "review_status", "review_quality"), exclude_cols = "instanceID", ... )
auth |
|
form_id |
String indicating id of the form. |
id_cols |
Character vector of data columns to keep in the result, will
be passed as |
exclude_cols |
Character vector of data columns to exclude from the result. |
... |
Additional arguments passed to |
A data.table with one row per response, i.e., one row per form
submission per field. If necessary to avoid ambiguity, columns from the
"survey" part of the form definition are prefixed with field_, while
columns from the "choices" part of the form definition are prefixed with
choice_. Original column names from data returned by scto_read() are
in the submission_field_name column, while corresponding values are in
the field_value column. Data can be converted back to wide format as
desired using data.table::dcast() or tidyr::pivot_wider().
## Not run: auth = scto_auth('scto_auth.txt') responses = scto_get_form_responses(auth, 'my_form') ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') responses = scto_get_form_responses(auth, 'my_form') ## End(Not run)
These functions read metadata from a SurveyCTO server.
scto_meta(auth) scto_catalog(auth)scto_meta(auth) scto_catalog(auth)
auth |
|
scto_meta() returns a nested list of metadata related to forms,
datasets, groups, and publishing information. scto_catalog() returns a
data.table with one row per form or dataset.
## Not run: auth = scto_auth('scto_auth.txt') metadata = scto_meta(auth) catalog = scto_catalog(auth) ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') metadata = scto_meta(auth) catalog = scto_catalog(auth) ## End(Not run)
By default, rsurveycto prints messages to the console. To suppress them, set
the rsurveycto_quiet option to TRUE or use this function.
scto_quiet(quiet = NULL)scto_quiet(quiet = NULL)
quiet |
A logical indicating whether to suppress messages, or |
If quiet is NULL, the current value of the rsurveycto_quiet
option. Otherwise, the previous value of the rsurveycto_quiet option
invisibly.
options(rsurveycto_quiet = TRUE) scto_quiet() scto_quiet(FALSE)options(rsurveycto_quiet = TRUE) scto_quiet() scto_quiet(FALSE)
This function can read datasets and forms.
scto_read( auth, ids = NULL, start_date = as.POSIXct("1900-01-01", tz = "UTC"), review_status = "approved", private_key = NULL, drop_empty_cols = FALSE, convert_datetime = c("CompletionDate", "SubmissionDate", "starttime", "endtime"), datetime_format = "%b %e, %Y %I:%M:%S %p", simplify = TRUE )scto_read( auth, ids = NULL, start_date = as.POSIXct("1900-01-01", tz = "UTC"), review_status = "approved", private_key = NULL, drop_empty_cols = FALSE, convert_datetime = c("CompletionDate", "SubmissionDate", "starttime", "endtime"), datetime_format = "%b %e, %Y %I:%M:%S %p", simplify = TRUE )
auth |
|
ids |
Character vector indicating ids of the datasets and/or forms.
|
start_date |
Date-time or something coercible to a date-time
indicating the earliest date-time (UTC timezone) for which to fetch data.
Only used for forms. Use with caution, because fields that are deleted
prior to |
review_status |
String or character vector indicating which submissions to fetch. Possible values are "approved", "pending", "rejected", or any combination of the three. Only used for forms. |
private_key |
String indicating path to private key file. Only needs to
be non- |
drop_empty_cols |
Logical indicating whether to drop columns that
contain only |
convert_datetime |
Character vector of column names in the data for
which to convert strings to datetimes (POSIXct). Use |
datetime_format |
String indicating format of datetimes from SurveyCTO.
See |
simplify |
Logical indicating whether to return only a |
If simplify is TRUE and reading one form or dataset, a
data.table. Otherwise a named list of data.tables, one for each form
and dataset, along with a data.table named ".catalog" from
scto_catalog().
## Not run: auth = scto_auth('scto_auth.txt') form_data = scto_read(auth, 'my_form') all_data = scto_read(auth) ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') form_data = scto_read(auth, 'my_form') all_data = scto_read(auth) ## End(Not run)
This function unnests form definitions, e.g., from multiple versions of a form, which can make it easier to map values to labels in a later step.
scto_unnest_form_definitions(form_metadata, by_form_id = TRUE)scto_unnest_form_definitions(form_metadata, by_form_id = TRUE)
form_metadata |
|
by_form_id |
Logical indicating whether to unnest definitions of multiple versions of a given form (default), or to unnest definitions of all forms together. |
If by_form_id is TRUE, a data.table of data.tables for the
survey, choices, and settings components of the form definitions. Otherwise
a list of data.tables.
## Not run: auth = scto_auth('scto_auth.txt') form_metadata = scto_get_form_metadata(auth, 'my_form') form_defs = scto_unnest_form_definitions(form_metadata) ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') form_metadata = scto_get_form_metadata(auth, 'my_form') form_defs = scto_unnest_form_definitions(form_metadata) ## End(Not run)
This function updates an existing dataset using an unofficial API endpoint. For this to work, the SurveyCTO user must have permissions "Can edit server datasets" and "Can modify or delete server dataset data".
scto_write( auth, data, dataset_id, dataset_title = dataset_id, append = FALSE, fill = FALSE )scto_write( auth, data, dataset_id, dataset_title = dataset_id, append = FALSE, fill = FALSE )
auth |
|
data |
|
dataset_id |
String indicating id of existing dataset. |
dataset_title |
String indicating title of dataset. Will replace the
existing title, regardless of |
append |
Logical indicating whether to append or replace the dataset. |
fill |
Logical indicating whether to implicitly fill missing columns
with |
A list with elements:
data_old: A data.table of the previous version of the dataset.
response: An object of class httr::response() from the API call.
## Not run: auth = scto_auth('scto_auth.txt') r = scto_write(auth, data, 'my_dataset', 'My Dataset') ## End(Not run)## Not run: auth = scto_auth('scto_auth.txt') r = scto_write(auth, data, 'my_dataset', 'My Dataset') ## End(Not run)