Skip to content

Commit dfdf8d7

Browse files
committed
Session work in progress - uncommitted changes
Includes modifications to: - DESCRIPTION - R/bf_encode.R, bf_flag.R, bf_map.R, bf_protocol.R - README files and documentation - New vignettes directory This commit captures the working state at end of debugging session.
1 parent 9789780 commit dfdf8d7

18 files changed

+2435
-26
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ Suggests:
4040
Depends:
4141
R (>= 4.1.0)
4242
Config/testthat/edition: 3
43+
VignetteBuilder: knitr

R/bf_encode.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#' reg <- bf_map(protocol = "na", data = bf_tbl, registry = reg, x = y)
1313
#'
1414
#' field <- bf_encode(registry = reg)
15-
#' @importFrom checkmate assertClass assertCharacter assertLogical
15+
#' @importFrom checkmate assertClass assertList
1616
#' @importFrom tibble tibble
1717
#' @importFrom purrr map map_int
18-
#' @importFrom dplyr bind_rows arrange bind_cols select across
18+
#' @importFrom dplyr bind_rows arrange bind_cols select across mutate
1919
#' @importFrom stringr str_split str_split_i str_sub str_pad str_remove
20-
#' @importFrom tidyr separate_wider_position
20+
#' @importFrom tidyr separate_wider_position unite
2121
#' @importFrom rlang `:=`
2222
#' @export
2323

@@ -55,22 +55,23 @@ bf_encode <- function(registry){
5555
# get the decimal part of the binary value and ...
5656
decBits <- .toBin(x = theVals, dec = TRUE)
5757

58-
# 1. transform to scientific notation, then ...
58+
# 1. transform to scientific notation...
5959
temp <- gsub("^(.{1})(.*)$", "\\1.\\2", str_remove(paste0(intBits, decBits), "^0+"))
60+
temp <- ifelse(temp == "", paste0(c(".", rep(0, theFlag$wasGeneratedBy$encodeAsBinary$significand)), collapse = ""), temp)
6061

61-
# 2. encode as bit sequence
62+
# 2. ... then encode as bit sequence
6263
sign <- as.integer(0 > theVals)
6364

6465
# 3. bias exponent and encode as binary
6566
exponent <- .toBin(x = nchar(str_remove(intBits, "^0+"))-1 + theFlag$wasGeneratedBy$encodeAsBinary$bias)
6667

6768
# 4. extract significand
6869
significand <- map(.x = temp,
69-
.f = \(x) str_sub(str_split(string = x, pattern = "[.]", simplify = TRUE)[2], end = theFlag$wasGeneratedBy$encodeAsBinary$significand )) |>
70+
.f = \(x) str_pad(str_sub(str_split(string = x, pattern = "[.]", simplify = TRUE)[2], end = theFlag$wasGeneratedBy$encodeAsBinary$significand), width = theFlag$wasGeneratedBy$encodeAsBinary$significand, pad = "0", side = "right")) |>
7071
unlist()
7172

7273
# 5. store as bit sequence
73-
theBits <- paste0(sign, exponent, significand )
74+
theBits <- paste0(sign, exponent, significand)
7475

7576
} else {
7677
theBits <- intBits

R/bf_flag.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bf_flag <- function(registry, flag = NULL) {
3535
} else {
3636
pcl <- get(protocol)
3737
}
38+
pcl$test <- paste0(deparse(pcl$test), collapse = "")
3839
pcl <- .validateProtocol(pcl)
3940

4041
# get NA value

R/bf_map.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
#' \item \code{integer} (x, ...): encode the integer values as bit-sequence
5151
#' (\emph{signed integer}).
5252
#' \item \code{numeric} (x, ...): encode the numeric value as floating-point
53-
#' bit-sequence (with an adapted precision) (\emph{floating-point}).
53+
#' bit-sequence (see \code{\link{.makeEncoding}} for details on the
54+
#' ... argument) (\emph{floating-point}).
5455
#' }
5556
#'
5657
#' @section Notes: The console output of various classes (such as tibble) shows
@@ -87,7 +88,7 @@
8788
#' reg <- bf_map(protocol = "matches", data = bf_tbl, registry = reg,
8889
#' x = commodity, set = c("soybean", "honey"))
8990
#' reg <- bf_map(protocol = "grepl", data = bf_tbl, registry = reg,
90-
#' x = year, pattern = "*r")
91+
#' x = year, pattern = ".*r")
9192
#'
9293
#' # enumeration encoding
9394
#' reg <- bf_map(protocol = "category", data = bf_tbl, registry = reg,
@@ -144,6 +145,7 @@ bf_map <- function(protocol, data, registry, ..., name = NULL, pos = NULL,
144145
na.val = NULL){
145146

146147
assertCharacter(x = protocol, len = 1, any.missing = FALSE)
148+
if(grepl(x = protocol, pattern = "_")) stop("protocol name ('", protocol, "'), must not contain '_' symbols.")
147149
assertClass(x = registry, classes = "registry")
148150
assertCharacter(x = name, len = 1, any.missing = FALSE, null.ok = TRUE)
149151
assertIntegerish(x = pos, lower = 1, min.len = 1, unique = TRUE, null.ok = TRUE)
@@ -154,10 +156,18 @@ bf_map <- function(protocol, data, registry, ..., name = NULL, pos = NULL,
154156
# load protocol ----
155157
if(protocol %in% names(bf_pcl)){
156158
pcl <- bf_pcl[[protocol]]
159+
# internal protocols already have string tests, convert to function for execution
160+
if (is.character(pcl$test)) {
161+
pcl$test <- eval(parse(text = pcl$test))
162+
}
157163
} else {
158164
pcl <- get(protocol)
165+
pcl <- .validateProtocol(pcl)
166+
# custom protocols need string->function conversion
167+
if (is.character(pcl$test)) {
168+
pcl$test <- eval(parse(text = pcl$test))
169+
}
159170
}
160-
pcl <- .validateProtocol(pcl)
161171

162172
# load potentially missing packages ----
163173
if(!is.null(pcl$requires)){

R/bf_protocol.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#' the operation in this protocol. It will be parsed with
66
#' \code{\link[glue]{glue}} and used in the bitfield legend, so can include
77
#' the test arguments as enbraced expressions.
8-
#' @param test [`function(...)`][function]\cr the function used to compute the bit
9-
#' flag.
8+
#' @param test [`function(...)`][character]\cr the function used to compute the bit
9+
#' flag (expressed as character string).
1010
#' @param example [`list(.)`][list]\cr named list that contains all arguments in
1111
#' \code{test} as name with values of the correct type.
1212
#' @param type [`character(1)`][character]\cr the encoding type according to
@@ -32,11 +32,11 @@
3232
#' @examples
3333
#' newFlag <- bf_protocol(name = "na",
3434
#' description = "{x} contains NA-values{result}.",
35-
#' test = function(x) is.na(x = x),
35+
#' test = "function(x) is.na(x = x)",
3636
#' example = list(x = bf_tbl$commodity),
3737
#' type = "bool")
38-
#' @importFrom checkmate assertCharacter assertFunction assertChoice
39-
#' assertIntegerish
38+
#' @importFrom checkmate assertCharacter assertFunction assertList assertChoice
39+
#' assertIntegerish assertClass
4040
#' @importFrom rlang exec
4141
#' @importFrom dplyr case_match case_when
4242
#' @importFrom utils bibentry
@@ -48,7 +48,7 @@ bf_protocol <- function(name, description, test, example, type, bits = NULL,
4848

4949
assertCharacter(x = name, len = 1, any.missing = FALSE)
5050
assertCharacter(x = description, len = 1, any.missing = FALSE)
51-
assertFunction(x = test)
51+
assertCharacter(x = test, len = 1, any.missing = FALSE)
5252
assertList(x = example)
5353
assertChoice(x = type, choices = c("bool", "enum", "int", "float"))
5454
assertIntegerish(x = bits, len = 1, null.ok = TRUE)
@@ -58,6 +58,8 @@ bf_protocol <- function(name, description, test, example, type, bits = NULL,
5858
version <- "1.0.0"
5959
}
6060

61+
test <- eval(parse(text = test))
62+
6163
# determine number of bits, if not given ----
6264
if (is.null(bits)) {
6365

@@ -94,7 +96,7 @@ bf_protocol <- function(name, description, test, example, type, bits = NULL,
9496
encoding_type = type,
9597
bits = bits, # when the test is general and could result in any number of bits, use NA here
9698
requires = requiredPkgs,
97-
test = paste0(deparse(test), collapse = ""),
99+
test = test,
98100
data = example,
99101
reference = reference)
100102

README.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bf_tbl |>
9090
knitr::kable()
9191
```
9292

93-
The decoded information is also available in the package environment for programmatic access:
93+
The decoded information is also available in the global environment for programmatic access:
9494

9595
```{r}
9696
# access values manually
@@ -107,7 +107,7 @@ bf_tbl$yield
107107

108108
- **[Best Practices](articles/best-practices.html)**: Guidelines for effective bitfield design, protocol selection, and common pitfalls
109109
- **[Community Contributions](articles/community-contributions.html)**: How to contribute protocols to the community standards repository
110-
- **[Applications and Citations](articles/applications.html)**: Papers and projects using the bitfield package
110+
- **[Applications](articles/applications.html)**: Examples and use cases using the bitfield package
111111

112112
## Getting Help
113113

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ bf_tbl$yield
171171
effective bitfield design, protocol selection, and common pitfalls
172172
- **[Community Contributions](articles/community-contributions.html)**:
173173
How to contribute protocols to the community standards repository
174-
- **[Applications and Citations](articles/applications.html)**: Papers
175-
and projects using the bitfield package
174+
- **[Applications](articles/applications.html)**: Examples and use cases
175+
using the bitfield package
176176

177177
## Getting Help
178178

man/bf_map.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bf_protocol.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/applications.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## ----include = FALSE----------------------------------------------------------
2+
knitr::opts_chunk$set(
3+
collapse = TRUE,
4+
comment = "#>"
5+
)
6+

0 commit comments

Comments
 (0)