Skip to content

Commit 071e500

Browse files
committed
teach xyzmatrix<- to assign to cols x,y,z
* includes test and docs
1 parent bc8c3ea commit 071e500

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

R/xform.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ xyzmatrix.dotprops<-function(x, ...) x$points
120120

121121
#' @method xyzmatrix default
122122
#' @param y,z separate y and z coordinates
123-
#' @details Note that \code{xyzmatrix.default} can extract 3d coordinates from a
124-
#' a \code{matrix} or \code{data.frame} that \bold{either} has exactly 3
125-
#' columns \bold{or} has 3 columns named X,Y,Z or x,y,z.
123+
#' @details Note that \code{xyzmatrix} can extract or set 3d coordinates in a a
124+
#' \code{matrix} or \code{data.frame} that \bold{either} has exactly 3 columns
125+
#' \bold{or} has 3 columns named X,Y,Z or x,y,z.
126126
#' @rdname xyzmatrix
127127
#' @export
128128
xyzmatrix.default<-function(x, y=NULL, z=NULL, ...) {
@@ -159,8 +159,9 @@ xyzmatrix.igraph<-function(x, ...){
159159
igraph::get.graph.attribute(x, 'xyz')
160160
}
161161

162-
#' @description Assign xyz elements of neuron or dotprops object. Can also
163-
#' handle matrix like objects with columns named X,Y,Z
162+
#' @description \code{xyzmatrix<-} assigns xyz elements of neuron or dotprops
163+
#' object and can also handle matrix like objects with columns named X, Y, Z
164+
#' or x, y, z.
164165
#' @usage xyzmatrix(x) <- value
165166
#' @param value Nx3 matrix specifying new xyz coords
166167
#' @return Original object with modified coords
@@ -177,10 +178,13 @@ xyzmatrix.igraph<-function(x, ...){
177178

178179
#' @export
179180
`xyzmatrix<-.default`<-function(x, value){
180-
if(is.neuron(x)) x$d[,c("X","Y","Z")]=value
181-
else if(is.dotprops(x)) x$points[,c("X","Y","Z")]=value
182-
else if(all(c("X","Y","Z") %in% colnames(x))) x[,c("X","Y","Z")]=value
183-
else stop("Not a neuron or dotprops object or a matrix-like object with XYZ volnames")
181+
xyzn=c("X","Y","Z")
182+
if(is.neuron(x)) x$d[,xyzn]=value
183+
else if(is.dotprops(x)) x$points[,xyzn]=value
184+
else if(!any(is.na(matched_cols<-match(xyzn, toupper(colnames(x)))))) {
185+
x[,matched_cols]=value
186+
}
187+
else stop("Not a neuron or dotprops object or a matrix-like object with XYZ colnames")
184188
x
185189
}
186190

man/xyzmatrix.Rd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ Original object with modified coords
3232
Get and assign coordinates for classes containing 3d vertex
3333
data
3434

35-
Assign xyz elements of neuron or dotprops object. Can also
36-
handle matrix like objects with columns named X,Y,Z
35+
\code{xyzmatrix<-} assigns xyz elements of neuron or
36+
dotprops object and can also handle matrix like objects
37+
with columns named X, Y, Z or x, y, z.
3738
}
3839
\details{
3940
Note that \code{xyzmatrix.default} can extract 3d

tests/testthat/test-xform.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ test_that("can extract xyz coords from a matrix",{
8282
expect_equal(xyzmatrix(df),data.matrix(df[,1:3]))
8383
})
8484

85+
test_that("can replace xyz coords of a matrix",{
86+
mx=matrix(1:24,ncol=3)
87+
colnames(mx)=c("X","Y","Z")
88+
mx2=mx
89+
colnames(mx2)=c("x","y","z")
90+
91+
expect_equivalent(xyzmatrix(mx)<-xyzmatrix(mx2), mx)
92+
})
93+
8594
test_that("can extract xyz coords from a neuronlist",{
8695
xyz12=rbind(xyzmatrix(kcs20[[1]]),xyzmatrix(kcs20[[2]]))
8796
expect_is(xyzmatrix(kcs20[1:2]),'matrix')

0 commit comments

Comments
 (0)