Skip to content

Commit

Permalink
✨ export generate_form()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Nov 10, 2020
1 parent 41137cd commit 71ef2b4
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 49 deletions.
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ export(fob_among)
export(fob_date)
export(fob_integer)
export(fob_numeric)
export(fob_pattern)
export(fob_text)
export(fob_word)
export(generate_form)
export(generate_form_choices)
export(generate_form_pattern)
export(join)
export(match_pattern)
export(multi)
importFrom(cli,style_bold)
importFrom(cli,style_underline)
Expand Down
4 changes: 0 additions & 4 deletions R/build_form.R

This file was deleted.

3 changes: 2 additions & 1 deletion R/one_among.R → R/fob_among.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param choices A vector of choices.
#' @param output Either "choice" or "id".
#' @param field_name Name of the field (optional).
#' @param ... further arguments passed to generate_form_choices().
#' @param ... further arguments to be passed to [generate_form_choices()].
#'
#' @return Return the choice selected either as the choice value or its identifier (an integer).
#' @export
Expand All @@ -24,6 +24,7 @@ fob_among <- function(question, choices, field_name = "", output = "choice",
question = question,
choices = choices,
field_name = field_name,
post = postf,
...
)

Expand Down
2 changes: 1 addition & 1 deletion R/one_integer.R → R/fob_integer.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @param field_name Name of the field (optional).
#' @param max maximum of characters (ignored if `NULL`).
#' @param format date format.
#' @param ... further argument.
#' @param ... further arguments to be passed to [generate_form_pattern()].
#'
#' @return Return the choice selected either as the choice value or its identifier (an integer).
#' @export
Expand Down
11 changes: 5 additions & 6 deletions R/match_pattern.R → R/fob_pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
#'
#' @param question The question to be asked.
#' @param pattern Pattern to be matched.
#' @param post Post processing.
#' @param field_name Name of the field (optional).
#' @param prompt the string printed when prompting the user for input (see [readline()]).
#' @param ... further arguments passed to [generate_form_pattern()].
#'
#' @return answer validated.
#' @export

match_pattern <- function(question, pattern, post = NULL, field_name = "",
prompt = "Enter your answer:") {
fob_pattern <- function(question, pattern, field_name = "",
prompt = "Enter your answer:", ...) {

generate_form_pattern(
question = question,
prompt = prompt,
pattern = pattern,
pre = NULL,
post = post,
field_name = field_name
field_name = field_name,
...
)

}
Expand Down
23 changes: 18 additions & 5 deletions R/form_unit.R → R/generate.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@

#' Generate form
#'
#' @param prompt The string printed when prompting the user for input (see [readline()]).
#' @param question The question to be asked.
#' @param choices A vector of choices.
#' @param validate A function used to validate the answer, if `NULL` then validation step is ignored.
#' @param field_name Name of the field (optional).
#' @param confirm a logical. Should the answer be confirmed (default is FALSE).
#' @param pre A function for pre processing.
#' @param post A function for post processing.
#' @param pattern pattern to be matched.
#'
#' @export

generate_form <- function(prompt = "", question = NULL, choices = NULL,
validate = NULL, confirm = FALSE, pre = NULL, post = NULL, field_name = "") {
Expand Down Expand Up @@ -43,7 +55,7 @@ generate_form <- function(prompt = "", question = NULL, choices = NULL,
valid()
} else {
if (!is.null(validate)) {
stop("validate should be a function or `NULL`")
stop("`validate` should be a function or `NULL`")
}
}

Expand All @@ -54,15 +66,16 @@ generate_form <- function(prompt = "", question = NULL, choices = NULL,
structure(out, class = "form_partial", field_name = field_name)
}



#' @describeIn generate_form match pattern.
#' @export
generate_form_pattern <- function(prompt = "", question = NULL, choices = NULL,
pattern = "*", confirm = FALSE, pre = NULL, post = NULL, field_name = "") {
vf <- function(x) grepl(pattern, x)
generate_form(prompt, question, choices, vf, confirm, pre, post, field_name)
}


#' @describeIn generate_form list fo choices.
#' @export
generate_form_choices <- function(prompt = "", question = NULL, choices,
confirm = FALSE, pre = NULL, post = NULL, field_name = "") {
vf <- function(x) return(x %in% as.character(seq_along(choices)))
Expand Down
9 changes: 8 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ get_confirmed <- function() {
grepl("^Y$|^YES$", toupper(x))
}

not_confirmed <- function() msgWarning('Not confirmed!')
not_confirmed <- function() msgWarning('Not confirmed!')


#
# month.name
# months(ISOdate(2004,1:12,1))
# weekdays(ISOdate(2004,1,5:11))
# Sys.setlocale("LC_TIME"e,"fr_FR") ;format(ISOdate(2004,1:12,1), "%B")
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@
[![R-CMD-check](https://github.com/inSileco/formbuildr/workflows/R-CMD-check/badge.svg)](https://github.com/inSileco/formbuildr/actions?query=workflow%3AR-CMD-check)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)

An R :package: to build forms with the command line interface.
An R :package: to readily build forms with the command line interface.


```R
R> q1 <- one_among("Fruits?", c("Apple", "Pear"), "fruit")
R> q2 <- yes_or_no("Be or not to be", "shake")
R> q3 <- one_date("When was that?", "date")
R> q4 <- match_pattern("2 letters + 1 digit", "^[A-Za-z]{2}[0-9]$")
R> q1 <- fob_among("Fruits?", c("Apple", "Pear"), "fruit", confirm = TRUE)
R> q2 <- fob_yorn("Be or not to be", "shake")
R> q3 <- fob_date("When was that?", "date")
R> q4 <- fob_pattern("2 letters + 1 digit", "^[A-Za-z]{2}[0-9]$")
R> myform <- q1 %+% q2 %+% q3 %+% q4
R> res <- myform()
Fruits? ❓
Multiple choices:
1 : Apple
2 : Pear
Enter your choice: 1
Confirmed?
[Y]es or [N]o: Y
validated!
Be or not to be
Enter [Y]es or [N]o: no
Enter [Y]es or [N]o: N
validated!
When was that? ❓
Enter your answer (a date %Y-%m-%d): 2000000
Validation failed, try again!
Enter your answer (a date %Y-%m-%d): 2020-12-01
Enter your answer (a date %Y-%m-%d): 2010-12-01
validated!
2 letters + 1 digit
Enter your answer:hh1
Enter your answer:000
Validation failed, try again!
Enter your answer:lo1
validated!

R> res
Expand All @@ -38,10 +40,10 @@ $shake
[1] FALSE

$date
[1] "2020-12-01"
[1] "2010-12-01"

$answer_4
[1] "hh1"
[1] "lo1"

attr(,"class")
[1] "form_answers"
Expand All @@ -54,8 +56,8 @@ the last line returns `answers.yaml` with the following content:
```yaml
fruit: Apple
shake: no
date: '2020-12-01'
answer_4: hh1
date: '2010-12-01'
answer_4: lo1
```
Alternatively, you can use `join(q1, q2, q3, q4)` to create the same form. Also,
Expand Down
4 changes: 2 additions & 2 deletions man/fob_among.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/fob_integer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/match_pattern.Rd → man/fob_pattern.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions man/generate_form.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
context("NULL")

q1 <- one_among("Fruits?", c("Apple", "Pear"), "fruit")
q2 <- one_yorn("Be or not to be", "shake")
q3 <- one_date("When was that?", "date")
q4 <- match_pattern("2 letters + 1 digit", "^[A-Za-z]{2}[0-9]$")
q1 <- fob_among("Fruits?", c("Apple", "Pear"), "fruit", confirm = TRUE)
q2 <- fob_yorn("Be or not to be", "shake")
q3 <- fob_date("When was that?", "date")
q4 <- fob_pattern("2 letters + 1 digit", "^[A-Za-z]{2}[0-9]$")
myform <- q1 %+% q2 %+% q3 %+% q4
# res <- myform()

0 comments on commit 71ef2b4

Please sign in to comment.