Skip to content

Commit 6c6015e

Browse files
committed
give xyzmatrix2str a sep argument
* probably more convenient for most people * roxygenise
1 parent 6b719f7 commit 6c6015e

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

R/xyzmatrix.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,22 @@ xyzmatrix.mesh3d<-function(x, ...){
255255
#' with an object to a character vector (by default comma separated).
256256
#' @param format A \code{\link{sprintf}} compatible format string. The default
257257
#' will give comma separated values.
258+
#' @param sep A character vector specifying a separator string. Overrides
259+
#' \code{format} when present. The default value of \code{format} is
260+
#' equivalent to \code{sep=","}.
258261
#' @examples
259262
#' head(xyzmatrix2str(kcs20[[1]]))
260263
#' head(xyzmatrix2str(kcs20[[1]], format="(%g;%g;%g)"))
261264
#' # if you want to process the xyz locations (here rounded to nearest nm)
262265
#' # you must extract them from complex objects yourself
263266
#' xyzmatrix2str(round(xyzmatrix(kcs20[[1]])*1000), format="%d,%d,%d")[1:3]
264-
xyzmatrix2str <- function(x, format="%g,%g,%g") {
267+
xyzmatrix2str <- function(x, format="%g,%g,%g", sep=NULL) {
265268
xyz=xyzmatrix(x)
269+
if(!is.null(sep)) {
270+
if(!checkmate::test_character(sep, len = 1))
271+
stop("If specified, sep must be a character vector of length 1")
272+
format=paste0("%g", sep, "%g", sep, "%g")
273+
}
266274
sprintf(format, xyz[,1], xyz[,2], xyz[,3])
267275
}
268276

man/nvertices.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/xyzmatrix.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-xyzmatrix.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ test_that("can extract xyz coords of a character vector",{
169169
expect_equal(xyzmatrix(empty_target), xyzmatrix(rep))
170170
})
171171

172+
test_that("can generate character representation of XYZ coords", {
173+
m=matrix(1:6, ncol=3, byrow = T)
174+
expect_equal(xyzmatrix2str(m),
175+
c("1,2,3", "4,5,6"))
176+
expect_equal(xyzmatrix2str(m), xyzmatrix2str(m, sep=","))
177+
expect_equal(xyzmatrix2str(m, sep=", "),
178+
c("1, 2, 3", "4, 5, 6"))
179+
expect_error(xyzmatrix2str(m, sep=0))
180+
expect_equal(xyzmatrix2str(m, format="(%g;%g;%g)"),
181+
c("(1;2;3)", "(4;5;6)"))
182+
})
172183

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

0 commit comments

Comments
 (0)