Introducing the R package robotoolbox

That something I would love to improve @manh_nguyen . For select multiple, what would be the ideal format, can you show me an example? Also for select_multiple I’m still trying to find the best way to have label/no label switch as in select_one

1 Like

Well. I’m not sure if my preference would be the same with others. When I export my data from kobo I always choose the option export select many question as single column as it reduces the size of my data as well.
About label/no labels, I don’t have any problems with that yet :wink:.

The original columns without 0/1 are also exported. You want an option to not also add the 0/1 columns ?

For e.g on this dummy examples, I have 5 pet types, as you can see we still have the column pet_type exported.

Rows: 5
Columns: 25
$ start                <dttm> 2022-05-09 18:31:40, 2022-05-09 1…
$ end                  <dttm> 2022-05-09 18:35:12, 2022-05-09 1…
$ today                <date> 2022-05-09, 2022-05-09, 2022-05-0…
$ full_name            <chr> "Rufus", "Romulus", "Remus", "Joe"…
$ pet_type             <chr> "3 4", "4", "5", NA, "3 4 5"
$ pet_type_1           <int> 0, 0, 0, NA, 0
$ pet_type_2           <int> 0, 0, 0, NA, 0
$ pet_type_3           <int> 1, 0, 0, NA, 1
$ pet_type_4           <int> 1, 1, 0, NA, 1
$ pet_type_5           <int> 0, 0, 1, NA, 1

I often don’t export o/1 columns in my data. But I think others prefer to have that columns and I can easily unselect the columns I don’t want in R script so it’s fine. Thank you for your hard work. It’s really nice package.

Thanks a lot @manh_nguyen, and don’t hesitate if you have ideas on how to improve.
I plan to push to CRAN soon.

Best,
Ahmadou

1 Like

Hello Dickoa, I was looking for an API for the Kobotoolbox and I came across yours. It is a wonderful package. I however have some problems, I wanted to host my Shinyapps on Shinyapps.io, unfortunately shinyapps.io does not recognize packages in gitlab repos. how can you help. is there a version on github?

@dan_mungai

Thanks for your message. We also have a mirror on github.

You can install it using remotes or pak

remotes::install_github("dickoa/robotoolbox")

Best,
Ahmadou

1 Like

Thank you so much
it has worked

1 Like

Hello Dickoa, The solution you provided for the previous problem worked. Now I have another problem. After updating my tidyr package, I am having this problem when I run this code: data ← kobo_asset_list()$uid[1] |> kobo_asset() |> kobo_data(),
The problem is
Error in unnest():
! Can’t rename variables in this context.
Run rlang::last_error() to see where the error occurred.

I would very much appreciate a solution.

<error/tidyselect:::error_disallowed_rename>
Error in unnest():
! Can’t rename variables in this context.

Backtrace:

  1. robotoolbox::kobo_data(kobo_asset(kobo_asset_list()$uid[1]))
  2. robotoolbox:::kobo_data.kobo_asset(kobo_asset(kobo_asset_list()$uid[1]))
  3. robotoolbox:::kobo_data_(…)
  4. robotoolbox:::kobo_form.kobo_asset(x)
  5. tidyr:::unnest.data.frame(survey, cols = is_list_cols(survey), keep_empty = TRUE)
    Run rlang::last_trace() to see the full context.

rlang::last_trace()
<error/tidyselect:::error_disallowed_rename>
Error in unnest():
! Can’t rename variables in this context.


Backtrace:

  1. ├─robotoolbox::kobo_data(kobo_asset(kobo_asset_list()$uid[1]))
  2. └─robotoolbox:::kobo_data.kobo_asset(kobo_asset(kobo_asset_list()$uid[1]))
  3. └─robotoolbox:::kobo_data_(…)
  4. ├─robotoolbox::kobo_form(x)
    
  5. └─robotoolbox:::kobo_form.kobo_asset(x)
    
  6.   ├─stats::setNames(...)
    
  7.   ├─tidyr::unnest(survey, cols = is_list_cols(survey), keep_empty = TRUE)
    
  8.   └─tidyr:::unnest.data.frame(survey, cols = is_list_cols(survey), keep_empty = TRUE)
    
  9.     └─tidyselect::eval_select(expr = enquo(cols), data = data, allow_rename = FALSE)
    
  10.       └─tidyselect:::eval_select_impl(...)
    
  11.         ├─tidyselect:::with_subscript_errors(...)
    
  12.         │ └─rlang::try_fetch(...)
    
  13.         │   └─base::withCallingHandlers(...)
    
  14.         └─tidyselect:::vars_select_eval(...)
    
  15.           └─tidyselect:::ensure_named(...)
    
  16.             └─cli::cli_abort(...)
    
  17.               └─rlang::abort(...)
    

Which version of tidyr are you using? I’ll see if it’s possible to reproduce it on my own.
Thanks

1 Like

@dan_mungai
Can you install the new version? I pushed a quick fix, and I’ll add some tests against dev versions of all dep in the future.
Thanks

1 Like

packageVersion(“tidyr”)
[1] ‘1.3.0’

Thanks, I made some changes. I also added some code to make sure that a call to kobo_settings() won’t print all your token (for safety).

remotes::install_github("dickoa/robotoolbox")

And let me know if it fixed the issue for you too.

Ahmadou

1 Like

it is fixed, thank you so much

2 Likes

my code has been working but I now get this error

Error in unnest():
! Can’t rename variables in this context.
Run rlang::last_error() to see where the error occurred.

rlang::last_error()
<error/tidyselect:::error_disallowed_rename>
Error in unnest():
! Can’t rename variables in this context.


Backtrace:

  1. robotoolbox::kobo_data(asset)
  2. robotoolbox:::kobo_data.kobo_asset(asset)
  3. robotoolbox:::kobo_data_(…)
  4. robotoolbox:::kobo_form.kobo_asset(x)
  5. tidyr:::unnest.data.frame(survey, cols = is_list_cols(survey), keep_empty = TRUE)
    Run rlang::last_trace() to see the full context.

rlang::last_trace()
<error/tidyselect:::error_disallowed_rename>
Error in unnest():
! Can’t rename variables in this context.


Backtrace:

  1. ├─robotoolbox::kobo_data(asset)
  2. └─robotoolbox:::kobo_data.kobo_asset(asset)
  3. └─robotoolbox:::kobo_data_(…)
  4. ├─robotoolbox::kobo_form(x)
    
  5. └─robotoolbox:::kobo_form.kobo_asset(x)
    
  6.   ├─stats::setNames(...)
    
  7.   ├─tidyr::unnest(survey, cols = is_list_cols(survey), keep_empty = TRUE)
    
  8.   └─tidyr:::unnest.data.frame(survey, cols = is_list_cols(survey), keep_empty = TRUE)
    
  9.     └─tidyselect::eval_select(expr = enquo(cols), data = data, allow_rename = FALSE)
    
  10.       └─tidyselect:::eval_select_impl(...)
    
  11.         ├─tidyselect:::with_subscript_errors(...)
    
  12.         │ └─rlang::try_fetch(...)
    
  13.         │   └─base::withCallingHandlers(...)
    
  14.         └─tidyselect:::vars_select_eval(...)
    
  15.           └─tidyselect:::ensure_named(...)
    
  16.             └─cli::cli_abort(...)
    
  17.               └─rlang::abort(...)
    

Hi @morriz,

Thanks for your message. Can you install the latest version and try again:

remotes::install_github("dickoa/robotoolbox")

Best,
Ahmadou

1 Like

Thanks it works properly…How do I change the values from xml values to labels


Thanks.

Hi @morriz ,

You can switch between xml and labels just for select_one for the moment. You can do it using the labelled R package. Let’s say you want the label for q1 you can use either labelled::to_character or labelled::to_factor.

library(robotoolbox)
library(tidyverse)
library(labelled)
library(dm)

data <- kobo_data(uid)
to_character(data$q1)
to_factor(data$q1)

data |>
  count(q1_label = to_character(q1))

data |>
  mutate(q1_label = to_factor(q1))

I would love to do the same for select_multiple but not sure about the way to approach this, if you have a suggestion, I would love to hear it. For the moment, the default for select_multiple is the original columns with choices separated by comma and a set of dummy columns (1/0) for each selected choices.

Thanks

1 Like

@dickoa,

It is wonderful that we now have an R package for this. I would like to use it, and have copied your code, but when running the script:

## install.packages("remotes")
remotes::install_gitlab("dickoa/robotoolbox")

I received the following error:

  • checking for file ‘C:\Users\xxx\robotoolbox-HEAD-dbe829e15b78e3bc97c23056216f05fc55ffee70/DESCRIPTION’ … OK
  • preparing ‘robotoolbox’:
  • checking DESCRIPTION meta-information … OK
  • checking for LF line-endings in source and make files and shell scripts
  • checking for empty or unneeded directories
  • building ‘robotoolbox_1.0.5.9000.tar.gz’
    Warning: invalid package ‘C:/Users/xxx/AppData/Local/Temp/Rtmp02iuFI/file30f8636f7782/robotoolbox_1.0.5.9000.tar.gz’
    Error: ERROR: no packages specified
    Warning: installation of package ‘C:/Users/xxx/AppData/Local/Temp/Rtmp02iuFI/file30f8636f7782/robotoolbox_1.0.5.9000.tar.gz’ had non-zero exit status

I have restared R and re-installed it, and re-installed the codes, but the error persists. Could you give some advice?