-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#define R_NO_REMAP | ||
#include <R.h> | ||
#include <Rinternals.h> | ||
#include "utils.h" | ||
|
||
SEXP lhs_name(SEXP x) { | ||
if (TYPEOF(x) != VECSXP) | ||
Rf_errorcall(R_NilValue, "`x` must be a list (not a %s)", Rf_type2char(TYPEOF(x))); | ||
|
||
int n = Rf_length(x); | ||
SEXP x2 = Rf_shallow_duplicate(x); | ||
|
||
SEXP names = Rf_getAttrib(x2, R_NamesSymbol); | ||
if (names == R_NilValue) { | ||
names = Rf_allocVector(STRSXP, n); | ||
Rf_setAttrib(x2, R_NamesSymbol, names); | ||
} | ||
|
||
for (int i = 0; i < n; ++i) { | ||
SEXP xi = VECTOR_ELT(x2, i); | ||
if (!is_formula(xi) || Rf_length(xi) != 3) | ||
continue; | ||
|
||
SEXP name = Rf_eval(lhs(xi), f_env(xi)); | ||
if (TYPEOF(name) != STRSXP || Rf_length(name) != 1) | ||
Rf_errorcall(R_NilValue, "LHS must evaluate to a single string"); | ||
|
||
SET_VECTOR_ELT(x2, i, Rf_lang2(CAR(xi), CADDR(xi))); | ||
SET_STRING_ELT(names, i, STRING_ELT(name, 0)); | ||
} | ||
|
||
return x2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
context("f_list") | ||
|
||
test_that("regular elements are left as is", { | ||
expect_equal(f_list(x = 1:10), list(x = 1:10)) | ||
expect_equal(f_list(x = ~x), list(x = ~x)) | ||
}) | ||
|
||
test_that("output always has names", { | ||
out <- f_list(1, 2, 3) | ||
expect_equal(names(out), c("", "", "")) | ||
}) | ||
|
||
test_that("names taken from LHS of formula", { | ||
out <- f_list("x" ~ y) | ||
expect_equal(out, list(x = ~y)) | ||
}) | ||
|
||
test_that("LHS evaluated in formula environment", { | ||
f <- function(x) { | ||
paste0(x, 1) ~ y | ||
} | ||
|
||
expect_equal(f_list(f("y")), list(y1 = ~ y)) | ||
}) |