Skip to content

fix [ on a worldArray with other than two layers - #58

Merged
achubaty merged 1 commit into
developmentfrom
fix-worldarray-layer-count
Aug 10, 2026
Merged

fix [ on a worldArray with other than two layers#58
achubaty merged 1 commit into
developmentfrom
fix-worldarray-layer-count

Conversation

@achubaty

Copy link
Copy Markdown
Collaborator

Found while investigating #49; unrelated to it and worth landing on its own.

[ on a worldArray hardcoded the layer count when reshaping the extracted values:

dim(cellValues) <- c(NROW(pCoords), 2L)

so it only ever worked on a two-layer array:

w1 <- createWorld(minPxcor = 0, maxPxcor = 4, minPycor = 0, maxPycor = 4, data = 1:25)
w2 <- createWorld(minPxcor = 0, maxPxcor = 4, minPycor = 0, maxPycor = 4, data = 26:50)
w3 <- createWorld(minPxcor = 0, maxPxcor = 4, minPycor = 0, maxPycor = 4, data = 51:75)

a3 <- stackWorlds(w1, w2, w3)
a3[1, 1]
#> Error: dims [product 2] do not match the length of object [3]

A single-layer stackWorlds(w1) failed the same way. Now uses dim(x)[3].

The sibling [,worldArray,missing,missing method already computed this correctly, which is why a3[] worked while a3[1, 1] did not — only the coordinate-indexed method was affected.

I checked the rest of the worldArray API against a three-layer array while I was in there: of(), NLset(), [<-, [[, numLayers(), layerNames(), plot() and world2spatRast() all already handled arbitrary layer counts. This was the only site.

Tests cover one, three and four layers.

Verification

  • R CMD check --as-cran: 0 errors, 0 warnings, 0 notes
  • devtools::test(): 0 failures

🤖 Generated with Claude Code

The number of layers was hardcoded when reshaping the extracted values,
so [ only ever worked on a two-layer worldArray:

  a3 <- stackWorlds(w1, w2, w3)
  a3[1, 1]
  #> Error: dims [product 2] do not match the length of object [3]

Use the array's own layer count instead. The sibling [,missing,missing
method already did this correctly, so only the coordinate-indexed one
was affected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@achubaty
achubaty merged commit 7df0ca4 into development Aug 10, 2026
14 checks passed
@achubaty
achubaty deleted the fix-worldarray-layer-count branch August 10, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant