Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string assign is too strict for use in nlmixr2est #779

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/parseCmtProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ static inline int handleRemainingAssignmentsCalcPropComplexAssign(nodeInfo ni, c
if (tb.lh[tb.ix] == isLHSstr ||
tb.lh[tb.ix] == isSuppressedLHSstr) {
D_ParseNode *xpn = d_get_child(pn, 2);
/* Free(v); */
const char* v2 = (char*)rc_dup_str(xpn->start_loc.s, xpn->end);
double d = 0.0;
int nd = sscanf(v2, "%lf", &d);
if (nd == 1) {
if (v2[0] == '-') return 1;
if (round(d) != d) {
errorStrAssign(v);
return 0;
Expand All @@ -211,9 +211,6 @@ static inline int handleRemainingAssignmentsCalcPropComplexAssign(nodeInfo ni, c
return 0;
}
}
} else {
errorStrAssign(v);
return 0;
}
} else if (tb.lh[tb.ix] == notLHS){
tb.lh[tb.ix] = isLHSparam;
Expand Down
9 changes: 8 additions & 1 deletion src/rxode2_df.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,18 @@ extern "C" SEXP rxode2_df(int doDose0, int doTBS) {
if (nlhs){
for (j = 0; j < nlhs; j++){
if (op->lhs_str[j] == 1) {
dfi = INTEGER(VECTOR_ELT(df, jj));
// factor; from string
IntegerVector cur = VECTOR_ELT(df, jj);
CharacterVector curL = cur.attr("levels");
dfi = INTEGER(cur);
int len = curL.size();
if (ISNA(ind->lhs[j])) {
dfi[ii] = NA_INTEGER;
} else {
dfi[ii] = (int)(ind->lhs[j]);
if (dfi[ii] < 1 || dfi[ii] > len) {
dfi[ii] = NA_INTEGER;
}
}
jj++;
} else {
Expand Down
45 changes: 45 additions & 0 deletions tests/testthat/test-lhs-str.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ f <- function() {
expect_error(rxode2parse('a <- "matt"; alag(a)<- 2'))
expect_error(rxode2parse("a <- \"str\"; a(0) <- -kel"))
expect_error(rxode2parse("a <- \"str\"; a(0) <- 1"))
# so that pruned expressions can work
expect_error(rxode2parse("a <- \"str\"; a <- 1+5"), NA)
expect_error(rxode2parse("a <- \"str\"; a <- -1+5"), NA)
expect_error(rxode2parse("a <- \"str\"; a <- +1+5"), NA)
}

test_that("test lhs string assign rxode2.syntax.allow.ini=TRUE", {
Expand Down Expand Up @@ -126,6 +130,47 @@ test_that("lhs solve; tests lhs assign & str equals with lhs", {
expect_true(all(s$b[s$time >= 10] == 1))
})

test_that("out of bounds solve gives NA for factors", {

rx <- rxode2({
if (t < 10) {
a <- "<10"
} else {
a <- ">=10"
}
a <- 1-3
b <- 1
if (a == "<10") {
b <- 0;
}
})

e <- et(1:20)

s <-rxSolve(rx, e, returnType = "data.frame")

expect_true(all(is.na(s$a)))

rx <- rxode2({
if (t < 10) {
a <- "<10"
} else {
a <- ">=10"
}
a <- 1+20
b <- 1
if (a == "<10") {
b <- 0;
}
})

s <-rxSolve(rx, e, returnType = "data.frame")

expect_true(all(is.na(s$a)))


})


test_that("lhs solve; tests lhs levels & str equals with lhs", {

Expand Down
Loading