Skip to contents

Kobotoolbox projects support multi-languages forms. It can be used to label questions and choices values in single or multiple-choice questions.

robotoolbox allows you to access labels from languages in your form. It uses the labelled package to encode variable labels for question labels and value labels for choice labels.

To illustrate its use, let’s load data from the following project, which uses three languages: English, French, and Arabic.

Survey questions

type name label::English (en) label::Francais (fr) label::Arabic (ar)
start start
end end
today today
text full_name What is your name? Quel est votre nom ? ما اسمك ؟
select_one yesno pet_yesno Do you have any pet? Avez-vous un animal de compagnie ? هل تمتلك حيوانا أليفا ؟

Choices

list_name name label::English (en) label::Francais (fr) label::Arabic (ar)
yesno 1 Yes Oui نعم
yesno 0 No Non لا

Loading the project asset

This project was uploaded on a Kobotoolbox server. It can be loaded into your session using its uid.

library(robotoolbox)
library(dplyr)
uid <- "aYuTZn9vegi3Z49MXwKjep"
asset <- kobo_asset(uid)
asset
#> <robotoolbox asset>  aYuTZn9vegi3Z49MXwKjep 
#>   Asset name: Multiple languages
#>   Asset type: survey
#>   Asset owner: dickoa
#>   Created: 2022-01-07 10:37:18
#>   Last modified: 2022-01-07 10:37:35
#>   Submissions: 5

Listing available languages

You can list all available languages in the project using the kobo_lang function.

kobo_lang(asset)
#> [1] "English (en)"  "Francais (fr)" "Arabic (ar)"

Reading data in each languages

We can use the lang parameter in the kobo_data function to specify which language to load. You can use kobo_lang to check the spelling of each language first.

df_en <- kobo_data(asset, lang = "English (en)")
df_fr <- kobo_data(asset, lang = "Francais (fr)")
df_ar <- kobo_data(asset, lang = "Arabic (ar)")
glimpse(df_en)
#> Rows: 5
#> Columns: 19
#> $ start                <chr> "2022-01-07T10:39:47.240-00:00", "2022-01-07T10:3…
#> $ end                  <chr> "2022-01-07T10:40:02.122-00:00", "2022-01-07T10:3…
#> $ today                <chr> "2022-01-07", "2022-01-07", "2022-01-07", "2022-0…
#> $ full_name            <chr> "Fatim", "Jean-Pierre", "إبراهيم", "Michelle", "A…
#> $ pet_yesno            <chr+lbl> "1", "0", "1", "1", "0"
#> $ `_id`                <int> 17735003, 17735002, 17735000, 17734995, 17734994
#> $ uuid                 <chr> "5388c680d19148828ea45913af820f30", "5388c680…
#> $ `__version__`        <chr> "vW6fMPTWKJmBfdSRM7jd4V", "vW6fMPTWKJmBfdSRM7jd4V…
#> $ instanceID           <chr> "uuid:03093de4-e32c-41b4-8f86-bd98f0cfd4e5", "uui…
#> $ `_xform_id_string`   <chr> "aYuTZn9vegi3Z49MXwKjep", "aYuTZn9vegi3Z49MXwKjep…
#> $ `_uuid`              <chr> "03093de4-e32c-41b4-8f86-bd98f0cfd4e5", "f3d0f43c…
#> $ `_attachments`       <list> <>, <>, <>, <>, <>
#> $ `_status`            <chr> "submitted_via_web", "submitted_via_web", "submit…
#> $ `_geolocation`       <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>
#> $ `_submission_time`   <chr> "2022-01-07T10:40:12", "2022-01-07T10:39:57", "20…
#> $ `_tags`              <list> <>, <>, <>, <>, <>
#> $ `_notes`             <list> <>, <>, <>, <>, <>
#> $ `_validation_status` <list> <>, <>, <>, <>, <>
#> $ `_submitted_by`      <lgl> NA, NA, NA, NA, NA

When the parameter is not set, it uses the default language. In this project, it’s English (en)

df_default <- kobo_data(asset)
all.equal(df_default, df_en)
#> [1] TRUE

Accessing variable labels

Questions labels can be accessed using var_label function from the labelled package.

library(labelled)
var_label(df_en$full_name)
#> [1] "What is your name?"
var_label(df_fr$full_name)
#> [1] "Quel est votre nom ?"
var_label(df_ar$full_name)
#> [1] "ما اسمك ؟"
var_label(df_en$pet_yesno)
#> [1] "Do you have any pet?"
var_label(df_fr$pet_yesno)
#> [1] "Avez-vous un animal de compagnie ?"
var_label(df_ar$pet_yesno)
#> [1] "هل تمتلك حيوانا أليفا ؟"

Accessing select_one type question labels

to_factor can be used to turn single choice questions values into labels.

table(to_factor(df_en$pet_yesno))
#> 
#> Yes  No 
#>   3   2
table(to_factor(df_fr$pet_yesno))
#> 
#> Oui Non 
#>   3   2
table(to_factor(df_ar$pet_yesno))
#> 
#> نعم  لا 
#>   3   2

to_character is an alternative to to_factor, when you prefer to use character instead of factor.

count(df_ar, pet_yesno_ar = to_character(pet_yesno))
#> # A tibble: 2 × 2
#>   pet_yesno_ar     n
#>   <chr>        <int>
#> 1 لا               2
#> 2 نعم              3

All these functions are from the labelled package, you can learn more about the package through its documentation.