Skip to content

Commit

Permalink
Fix and test xyzmatrix<- for 0 row dataframes
Browse files Browse the repository at this point in the history
* closes #433
* @alexanderbates this came up because some of your neuros have 0 row connector data.frames
  • Loading branch information
jefferis committed Apr 26, 2020
1 parent bc4452f commit df515c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/xform.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ xyzmatrix.mesh3d<-function(x, ...){

#' @export
`xyzmatrix<-.default`<-function(x, value){
# count number of elements in matrices/data.frames and vectors
nelems <- function(y) {
dy=dim(y)
if(is.null(dy)) length(y) else prod(dy)
}

# short circuit if x and value have no elements
if(isTRUE(nrow(x)==0 && nelems(value)==0))
return(x)

xyzn=c("X","Y","Z")
if(ncol(x)==3) {
x[,]=value
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-xform.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ test_that("can replace xyz coords of a matrix",{
expect_equal(mx3, mx3.saved)
})

test_that("can replace xyz coords of a data.frame",{
# we just need to handle some edge cases with 0 row data here

df <- df2 <- data.frame(x=numeric(), y=numeric(), z=numeric())
expect_silent(xyzmatrix(df) <- xyzmatrix(df)+1)
# check no change
expect_equal(df, df2)
# nb generates both error and warning
expect_warning(expect_error(xyzmatrix(df) <- 1:3))
})


test_that("can extract xyz coords from a neuronlist",{
xyz12=rbind(xyzmatrix(kcs20[[1]]),xyzmatrix(kcs20[[2]]))
expect_is(xyzmatrix(kcs20[1:2]),'matrix')
Expand Down

0 comments on commit df515c0

Please sign in to comment.