Skip to content

Commit

Permalink
add dim.x3p
Browse files Browse the repository at this point in the history
  • Loading branch information
heike committed Mar 22, 2024
1 parent 8c541d7 commit 5f7b9c7
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 19 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(dim,x3p)
S3method(head,x3p)
S3method(image,x3p)
S3method(print,x3p)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
## minor functionality

* `x3p_read_folder` allows an import of multiple x3p files (recursively) into a single data frame (in tibble format) with a list variable of x3p objects.
* `dim.x3p` just for convenience.

## bug fixes

* fix to `x3p_rotate` in an obscure situation


# x3ptools 0.0.4
Expand Down
34 changes: 34 additions & 0 deletions R/generics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Raster image of an x3p surface
#'
#' `image.x3p` expands the generic image method for x3p objects.
#' This image function creates a raster image to show the surface of an x3p file.
#' Due to some inconsistency in the mapping of the origin (0,0), (choice between top left or bottom left) image functions from different packages will result in different images.
#' @param x an x3p object
#' @param ... parameters passed into image
#' @importFrom graphics image
#' @method image x3p
#' @export
#' @examples
#' logo <- x3p_read(system.file("csafe-logo.x3p", package="x3ptools"))
#' image(logo)
image.x3p <- function(x, ...) {
graphics::image(x$surface.matrix, ylim = c(1, 0), ...)
}


#' Dimension of an x3p object
#'
#' `dim.x3p` expands the generic dim method for x3p objects and returns (mainly) the dimensions of the surface matrix.
#' @param x an x3p object
#' @method dim x3p
#' @returns a vector of length three. The first two integers contain width and height of the surface scan, the third dimension is either 1 or 2, depending on whether there is a mask (2) or not (1).
#' @export
#' @examples
#' logo <- x3p_read(system.file("csafe-logo.x3p", package="x3ptools"))
#' dim(logo)
dim.x3p <- function(x) {
matrix_dims <- dim(x$surface.matrix)
z <- 1
if (!is.null(x$mask)) z <- 2
c(matrix_dims, z)
}
16 changes: 0 additions & 16 deletions R/x3p_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,3 @@ x3p_snapshot <- function(file) {
}
}

#' Raster image of an x3p surface
#'
#' `image.x3p` expands the generic image method for x3p objects.
#' This image function creates a raster image to show the surface of an x3p file.
#' Due to some inconsistency in the mapping of the origin (0,0), (choice between top left or bottom left) image functions from different packages will result in different images.
#' @param x an x3p object
#' @param ... parameters passed into image
#' @importFrom graphics image
#' @method image x3p
#' @export
#' @examples
#' logo <- x3p_read(system.file("csafe-logo.x3p", package="x3ptools"))
#' image(logo)
image.x3p <- function(x, ...) {
graphics::image(x$surface.matrix, ylim = c(1, 0), ...)
}
21 changes: 21 additions & 0 deletions man/dim.x3p.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/image.x3p.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/x3p_image.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/x3p_snapshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ library(x3ptools)


test_check("x3ptools")

13 changes: 13 additions & 0 deletions tests/testthat/test_generics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
context("generics_x3p")

#'
#' dim(logo)

test_that("dim.x3p works as expected", {
logo <- x3p_read(system.file("csafe-logo.x3p", package="x3ptools"))
expect_equal(dim(logo), c(741, 419, 1))

expect_equal(dim(x3ptest_mask), c(6, 7, 2))

})

0 comments on commit 5f7b9c7

Please sign in to comment.