Skip to content

inSileco/formbuildr

Repository files navigation

formbuildr

R-CMD-check codecov lifecycle

An R 📦 to quickly build forms with the command line interface.

Simple creation of forms

R> q1 <- fob_among("Fruits?", c("Apple", "Pear"), "fruit", confirm = TRUE)
R> q2 <- fob_yorn("Be or not to be", "shake")
R> s1 <- fob_section("Next part")
R> q3 <- fob_date("When was that?", "date")
R> q4 <- fob_pattern("2 letters + 1 digit", "^[A-Za-z]{2}[0-9]$")
R> f1 <- fob_output(function(fruit, date, ...) paste0("On ", date, ", I ate one ", tolower(fruit)))
R> f2 <- fob_output(function(shake, answer_5, ...) ifelse(shake, answer_5, "none"), "shake")
R> myform <- q1 %+% q2 %+% s1 %+% q3 %+% q4 %+% f1 %+% f2
R> res <-  myform()❓ Fruits?
ℹ Multiple choices:
1: Apple
2: Pear
Enter your choice:  1Confirmed?
[Y]es or [N]o: Yvalidated!Be or not to be
Enter [Y]es or [N]o: Nvalidated!Next partWhen was that?
Enter your answer (a date %Y-%m-%d):  2020-10-10validated!2 letters + 1 digit
Enter your answer: ok1validated!

R> res
$answers
$answers$fruit
[1] "Apple"

$answers$shake
[1] FALSE

$answers$date
[1] "2020-10-10"

$answers$answer_5
[1] "ok1"


$outputs
$outputs$output_1
[1] "On 2020-10-10, I ate one apple"

$outputs$output_2
[1] "none"


attr(,"class")
[1] "form_answers"

Alternatively, you can use join(q1, q2, q3, q4) to create the same form. Note that forms can also be combined, just as questions, so what follows

R> q5 <- fob_int("How many?")
R> myform2 <- myform %+% q5

adds one question to the previous form, the answer to which should be an integer. Also, see multi() to repeat the form, e.g multi(myform, 2).

Form from file

See form_from_file().

Exporting results

So far, results are returned as a list and can be exported as "YAML" using export_answer(). For instance the following

R> export_answer(res, "answers.yaml")   

creates answers.yaml with the following content:

answers:
  fruit: Apple
  shake: no
  date: '2020-10-10'
  answer_2: ok1
outputs:
  output_1: On 2020-10-10, I ate one apple
  output_2: none

Direct validation

Once a form created it can be also used for direct validation, i.e. one can skip the interactive parts a check whether answers are correctly formatted:

R> myform2(1, "Y", "2010-01-01", "ok1", 1)
$answers
$answers$fruit
[1] "Apple"

$answers$shake
[1] TRUE

$answers$date
[1] "2010-01-01"

$answers$answer_5
[1] "ok1"

$answers$answer_6
[1] 1


$outputs
$outputs$output_1
[1] "On 2010-01-01, I ate one apple"

$outputs$output_2
[1] "ok1"


attr(,"class")
[1] "form_answers"

Currently NA is returned when the format is not correct

myform2(1, "Y", "2010-01-01", "ok1", 1.1)                        
$answers
$answers$fruit
[1] "Apple"

$answers$shake
[1] TRUE

$answers$date
[1] "2010-01-01"

$answers$answer_5
[1] "ok1"

$answers$answer_6
[1] NA


$outputs
$outputs$output_1
[1] "On 2010-01-01, I ate one apple"

$outputs$output_2
[1] "ok1"


attr(,"class")
[1] "form_answers"

Meta

About

R package to build form using the command line interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages