Skip to content

Commit 2301f3e

Browse files
committed
Handle blank indices in simplifyForLoops
1 parent 2cbdbce commit 2301f3e

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: nimbleMacros
2-
Version: 0.1.1.9002
3-
Date: 2026-04-30
2+
Version: 0.1.1.9003
3+
Date: 2026-06-09
44
Title: Macros Generating 'nimble' Code
55
Authors@R: c(person("Ken", "Kellner", email="contact@kenkellner.com",
66
role=c("cre","aut")),

R/utilities.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ simplifyIndices <- function(code, new_indices){
195195
if(is.name(x) | is.symbol(x)) return(x)
196196
if(x[[1]] == "for"){
197197
unique_idx <- unique(extractAllIndices(x))
198+
# Ignore blank indices as in x[i,]
199+
unique_idx <- unique_idx[sapply(unique_idx, function(x) x!="")]
198200
if(length(unique_idx) > length(new_indices)){
199201
stop("Not enough new indices provided", call.=FALSE)
200202
}

tests/testthat/test_simplifyForLoops.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,32 @@ test_that("More complex case of an occupancy model", {
123123
expect_error(simplifyForLoops(mod$getCode(), new_indices=list(quote(i))),
124124
"Not enough new indices provided")
125125
})
126+
127+
test_that("Blank index slot is ignored", {
128+
# e.g. in x[1:n,] we should not add a new index letter for the ,]
129+
# presumably this would occur when you are planning to add the dimensions
130+
# to the dimensions argument manually
131+
132+
test <- nimbleCode({
133+
for (i in 1:3){
134+
x[i] <- y[i] + 1
135+
}
136+
137+
for (j in 1:3){
138+
z[j,] <- 0
139+
}
140+
141+
})
142+
143+
out <- simplifyForLoops(test)
144+
145+
expect_equal(out,
146+
quote({
147+
for (i in 1:3){
148+
x[i] <- y[i] + 1
149+
z[i,] <- 0
150+
}
151+
})
152+
)
153+
})
154+

0 commit comments

Comments
 (0)