Skip to content

Commit 355a03e

Browse files
authored
Merge pull request #5 from nyuglobalties/feature/recoding-improvements
Add blueprintr variable decoration support
2 parents 8c6c0b3 + 5659862 commit 355a03e

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: rcoder
22
Type: Package
33
Title: Lightweight Data Structure for Recoding Categorical Data without Factors
4-
Version: 0.1.1
4+
Version: 0.1.2
55
Authors@R:
66
c(person(
77
given = "Patrick",

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# rcoder 0.1.2
2+
3+
* Adds {blueprintr} variable decoration support during assigning coding or recoding of vectors
4+
15
# rcoder 0.1.1
26

37
* Adds `recode_vec` and `assign_coding`, simple interfaces for recoding vectors and embedding codings as attributes in vectors, respectively

R/recoding.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@ make_recode_query <- function(linked_codings, from = 1, to_suffix = "to", ...) {
8282
#' of the vector. Defaults to the "rcoder.coding" attribute value
8383
#' @param .embed If `TRUE`, `from` will be stored in the "rcoder.coding"
8484
#' attribute
85+
#' @param .bpr If `TRUE`, adds the _character_ representation of
86+
#' the coding to the "bpr.coding" attribute. Used for interop with
87+
#' blueprintr variable decorations
8588
#' @return The recoded vector
8689
#' @export
8790
recode_vec <- function(
8891
vec,
8992
to,
9093
from = get_attr(vec, "rcoder.coding"),
91-
.embed = TRUE
94+
.embed = TRUE,
95+
.bpr = TRUE
9296
) {
9397
if (is.null(from)) {
9498
rc_err("Use `rcoder::assign_coding` to embed a `coding` to a vector")
@@ -101,14 +105,13 @@ recode_vec <- function(
101105
"{substitute(from)} is not a `coding`"
102106
)
103107

104-
105108
linked <- link_codings(to, from)
106109
recode_func <- make_recode_query(linked)
107110

108111
recoded_vec <- recode_func(vec)
109112

110113
if (isTRUE(.embed)) {
111-
recoded_vec <- assign_coding(recoded_vec, to)
114+
recoded_vec <- assign_coding(recoded_vec, to, .bpr)
112115
}
113116

114117
recoded_vec
@@ -120,11 +123,18 @@ recode_vec <- function(
120123
#'
121124
#' @param vec A vector
122125
#' @param .coding A `coding` object
126+
#' @param .bpr Also overwrite the "bpr.coding" attribute with the character
127+
#' representation of `.coding`. Used for interop with blueprintr
128+
#' variable decorations.
123129
#' @return The vector with its "rcoder.coding" attribute set to `.coding`
124130
#' @export
125-
assign_coding <- function(vec, .coding) {
131+
assign_coding <- function(vec, .coding, .bpr = TRUE) {
126132
rc_assert(is.atomic(vec), "{substitute(vec)} must be a vector")
127133
rc_assert(is.coding(.coding), "{substitute(.coding)} must be a `coding`")
128134

129-
set_attrs(vec, rcoder.coding = .coding)
135+
set_attrs(
136+
vec,
137+
rcoder.coding = .coding,
138+
bpr.coding = if (isTRUE(.bpr)) as.character(.coding) else NULL
139+
)
130140
}

man/assign_coding.Rd

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

man/recode_vec.Rd

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

tests/testthat/test-recoding.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ test_that("Vector recoding works", {
8484
vec <- assign_coding(vec, coding_1)
8585
expect_true(!is.null(get_attr(vec, "rcoder.coding")))
8686
expect_identical(get_attr(vec, "rcoder.coding"), coding_1)
87+
expect_identical(
88+
get_attr(vec, "bpr.coding"),
89+
as.character(coding_1)
90+
)
8791

8892
coding_2 <- coding(
8993
# Using 10 & 11 for no common value overlap
9094
code("Uncommon", 10, links_from = c("Never", "Rarely")),
9195
code("Common", 11, links_from = c("Sometimes", "Frequently"))
9296
)
9397

94-
vec <- recode_vec(vec, to = coding_2)
98+
vec <- recode_vec(vec, to = coding_2, .bpr = FALSE)
9599
expect_identical(get_attr(vec, "rcoder.coding"), coding_2)
100+
expect_null(get_attr(vec, "bpr.coding"))
96101
expect_true(all(vec %in% c(10, 11)))
97102
})

0 commit comments

Comments
 (0)