Skip to content

Commit c6cd10e

Browse files
authored
Merge pull request #30 from tdhock/alevels
add alevels, tests, examples, version++
2 parents d135f09 + 93bb9f4 commit c6cd10e

File tree

7 files changed

+144
-3
lines changed

7 files changed

+144
-3
lines changed

.github/workflows/test-coverage.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
1616

1717
steps:
18-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1919

2020
- uses: r-lib/actions/setup-r@v2
2121
with:
@@ -44,7 +44,7 @@ jobs:
4444

4545
- name: Upload test results
4646
if: failure()
47-
uses: actions/upload-artifact@v3
47+
uses: actions/upload-artifact@v4
4848
with:
4949
name: coverage-test-failures
5050
path: ${{ runner.temp }}/package

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Authors@R: c(
33
person("Toby", "Hocking",
44
email="toby.hocking@r-project.org",
55
role=c("aut", "cre")))
6-
Version: 2025.1.21
6+
Version: 2025.3.24
77
License: GPL-3
88
Title: Named Capture to Data Tables
99
Description: User-friendly functions for extracting a data

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export(alevels)
12
export(altlist)
23
export(alternatives_with_shared_groups)
34
export(measure)

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Changes in version 2025.3.24
2+
3+
- alevels() helper creates a pattern string that matches literal alternatives, and a conversion function that outputs a factor.
4+
15
Changes in version 2025.1.21
26

37
- capture_first_vec, capture_all_str, capture_first_df, capture_first_glob, measure, capture_melt_single, capture_melt_multiple now support type.convert argument. TRUE means to use utils::type.convert(x,as.is=TRUE) as default conversion function (as.is=TRUE means to return character instead of factor), FALSE means identity, and otherwise can be any function to use as default conversion.

R/alevels.R

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
alevels <- structure(function # Alternative levels
2+
### Create a pattern and a conversion function based on alternative
3+
### string literals.
4+
(...
5+
### Optional names are string literals to match; values are
6+
### corresponding factor levels.
7+
){
8+
old2new <- c(...)
9+
if(is.null(names(old2new))){
10+
names(old2new) <- old2new
11+
}
12+
to.rep <- names(old2new)==""
13+
names(old2new)[to.rep] <- old2new[to.rep]
14+
list(
15+
paste(names(old2new), collapse="|"),
16+
function(x)factor(old2new[x], old2new))
17+
### List of pattern and conversion function that returns factor.
18+
}, ex=function(){
19+
20+
## Example 0: melt iris data with literal alternatives -> chr columns.
21+
ichr <- nc::capture_melt_single(
22+
iris[1,],
23+
part="Sepal|Petal",
24+
"[.]",
25+
dim="Length|Width")
26+
factor(ichr$part)#default factor levels are alphabetical.
27+
28+
## Example 1: melt iris data with alevels() -> factor columns.
29+
(ifac <- nc::capture_melt_single(
30+
iris[1,],
31+
part=nc::alevels("Sepal","Petal"),
32+
"[.]",
33+
dim=nc::alevels("Length","Width")))
34+
ifac$part #factor with levels in same order as given in alevels().
35+
36+
## Example 2: alevels(literals_to_match="levels_to_use_in_output").
37+
tv_wide <- data.frame(
38+
id=0,
39+
train.classif.logloss = 1, train.classif.ce = 2,
40+
valid.classif.logloss = 3, valid.classif.ce = 4)
41+
nc::capture_melt_single(
42+
tv_wide,
43+
set=nc::alevels(valid="validation", train="subtrain"),
44+
"[.]classif[.]",
45+
measure=nc::alevels(ce="error_prop", auc="AUC", "logloss"))
46+
47+
## Example 3: additional groups which output character columns.
48+
nc::capture_melt_single(
49+
tv_wide,
50+
set_chr=list(set_fac=nc::alevels(valid="validation", train="subtrain")),
51+
"[.]classif[.]",
52+
measure_chr=list(measure_fac=nc::alevels(ce="error_prop", auc="AUC", "logloss")))
53+
54+
})
55+

man/alevels.Rd

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
\name{alevels}
2+
\alias{alevels}
3+
\title{Alternative levels}
4+
\description{Create a pattern and a conversion function based on alternative
5+
string literals.}
6+
\usage{alevels(...)}
7+
\arguments{
8+
\item{\dots}{Optional names are string literals to match; values are
9+
corresponding factor levels.}
10+
}
11+
12+
\value{List of pattern and conversion function that returns factor.}
13+
14+
\author{Toby Hocking <toby.hocking@r-project.org> [aut, cre]}
15+
16+
17+
18+
19+
\examples{
20+
21+
## Example 0: melt iris data with literal alternatives -> chr columns.
22+
ichr <- nc::capture_melt_single(
23+
iris[1,],
24+
part="Sepal|Petal",
25+
"[.]",
26+
dim="Length|Width")
27+
factor(ichr$part)#default factor levels are alphabetical.
28+
29+
## Example 1: melt iris data with alevels() -> factor columns.
30+
(ifac <- nc::capture_melt_single(
31+
iris[1,],
32+
part=nc::alevels("Sepal","Petal"),
33+
"[.]",
34+
dim=nc::alevels("Length","Width")))
35+
ifac$part #factor with levels in same order as given in alevels().
36+
37+
## Example 2: alevels(literals_to_match="levels_to_use_in_output").
38+
tv_wide <- data.frame(
39+
id=0,
40+
train.classif.logloss = 1, train.classif.ce = 2,
41+
valid.classif.logloss = 3, valid.classif.ce = 4)
42+
nc::capture_melt_single(
43+
tv_wide,
44+
set=nc::alevels(valid="validation", train="subtrain"),
45+
"[.]classif[.]",
46+
measure=nc::alevels(ce="error_prop", auc="AUC", "logloss"))
47+
48+
## Example 3: additional groups which output character columns.
49+
nc::capture_melt_single(
50+
tv_wide,
51+
set_chr=list(set_fac=nc::alevels(valid="validation", train="subtrain")),
52+
"[.]classif[.]",
53+
measure_chr=list(measure_fac=nc::alevels(ce="error_prop", auc="AUC", "logloss")))
54+
55+
}

tests/testthat/test-CRAN-alternatives.R

+26
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,29 @@ test_that("alternatives_with_shared_groups ok with 1 subject", {
172172
expect_identical(match.dt[["day"]], "17")
173173
expect_identical(match.dt[["year"]], "1983")
174174
})
175+
176+
test_that("alevels ok with no names", {
177+
ifac <- nc::capture_melt_single(
178+
iris[1,],
179+
part=nc::alevels("Sepal","Petal"),
180+
"[.]",
181+
dim=nc::alevels("Length","Width"))
182+
expect_identical(levels(ifac$part), c("Sepal","Petal"))
183+
expect_identical(levels(ifac$dim), c("Length","Width"))
184+
})
185+
186+
test_that("alevels() ok with all and some names", {
187+
tv_wide <- data.frame(
188+
id=0,
189+
train.classif.logloss = 1, train.classif.ce = 2,
190+
valid.classif.logloss = 3, valid.classif.ce = 4)
191+
tv_long <- nc::capture_melt_single(
192+
tv_wide,
193+
set_chr=list(set_fac=nc::alevels(valid="validation", train="subtrain")),
194+
"[.]classif[.]",
195+
measure_chr=list(measure_fac=nc::alevels(ce="error_prop", auc="AUC", "logloss")))
196+
expect_is(tv_long$set_chr, "character")
197+
expect_is(tv_long$measure_chr, "character")
198+
expect_identical(levels(tv_long$set_fac), c("validation","subtrain"))
199+
expect_identical(levels(tv_long$measure_fac), c("error_prop","AUC","logloss"))
200+
})

0 commit comments

Comments
 (0)