Skip to content

Commit bc8c3ea

Browse files
committed
Teach xyzmatrix to cope with lower case x,y,z colnames
* includes test and docs
1 parent 4f82525 commit bc8c3ea

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

R/xform.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +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.
123126
#' @rdname xyzmatrix
124127
#' @export
125128
xyzmatrix.default<-function(x, y=NULL, z=NULL, ...) {
@@ -130,7 +133,8 @@ xyzmatrix.default<-function(x, y=NULL, z=NULL, ...) {
130133
x=cbind(x,y,z)
131134
} else if(is.data.frame(x) || is.matrix(x)){
132135
if(ncol(x)>3){
133-
if(all(xyzn%in%colnames(x))) x=x[,xyzn]
136+
matched_cols=match(xyzn, toupper(colnames(x)))
137+
if(!any(is.na(matched_cols))) x=x[,matched_cols]
134138
else stop("Ambiguous column names. Unable to retrieve XYZ data")
135139
} else if(ncol(x)<3) stop("Must have 3 columns of XYZ data")
136140
}

man/xyzmatrix.Rd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ data
3535
Assign xyz elements of neuron or dotprops object. Can also
3636
handle matrix like objects with columns named X,Y,Z
3737
}
38+
\details{
39+
Note that \code{xyzmatrix.default} can extract 3d
40+
coordinates from a a \code{matrix} or \code{data.frame}
41+
that \bold{either} has exactly 3 columns \bold{or} has 3
42+
columns named X,Y,Z or x,y,z.
43+
}
3844
\examples{
3945
n=Cell07PNs[[1]]
4046
xyzmatrix(n)<-xyzmatrix(n)

tests/testthat/test-xform.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ test_that("can extract xyz coords from a matrix",{
7474
expect_equivalent(xyzmatrix(mx),mx)
7575
colnames(mx)=c("X","Y","Z")
7676
expect_equal(xyzmatrix(mx),mx)
77+
mx2=mx
78+
colnames(mx2)=c("x","y","z")
79+
expect_equal(xyzmatrix(mx2),mx)
7780

7881
df=data.frame(X=1:4,Y=2:5,Z=3:6,W=1)
7982
expect_equal(xyzmatrix(df),data.matrix(df[,1:3]))

0 commit comments

Comments
 (0)