tests/testthat/test-im.pca.R

test_that("multiplication works", {
  expect_equal(2 * 2, 4)
})

library(testthat)
library(imageRy)
library(terra)

test_that("im.pca correctly performs Principal Component Analysis (PCA)", {
  # Create a raster with three bands (simulating multispectral data)
  r <- rast(nrows = 10, ncols = 10, nlyrs = 3)
  values(r) <- runif(300)  # Assign random values to simulate image data

  # Perform PCA
  pca_result <- im.pca(r, n_samples = 100)

  # Ensure that the result is a `SpatRaster`
  expect_s4_class(pca_result, "SpatRaster")

  # Ensure that the number of layers in the result matches the number of input bands
  expect_equal(nlyr(pca_result), nlyr(r))

  # Check that the values are not NULL
  expect_false(any(is.na(values(pca_result))))
})

Try the imageRy package in your browser

Any scripts or data that you put into this service are public.

imageRy documentation built on Aug. 8, 2025, 7:31 p.m.