From 12c8912436570a9b340984bf087f1ef1c2eb779c Mon Sep 17 00:00:00 2001 From: geryan Date: Mon, 15 Jul 2024 10:34:17 +1000 Subject: [PATCH] The glamour subdue me --- NAMESPACE | 1 + R/set_levels.R | 37 +++++++++++++++++++++++++++++++++++++ man/set_levels.Rd | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 R/set_levels.R create mode 100644 man/set_levels.Rd diff --git a/NAMESPACE b/NAMESPACE index 3d958b3..821c4d9 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(predict_sdm) export(rastpointplot) export(save_plot) export(set_layer_names) +export(set_levels) export(source_R) export(split_rast) export(standardise_rast) diff --git a/R/set_levels.R b/R/set_levels.R new file mode 100644 index 0000000..42239f0 --- /dev/null +++ b/R/set_levels.R @@ -0,0 +1,37 @@ +#' @title set levels +#' @description +#' Convenience function for setting `SpatRaster` levels names to enable setting in +#' piped workflows. +#' +#' +#' @param x `SpatRaster` +#' @param levs `data.frame` +#' +#' @return `SpatRaster` +#' @export +#' +#' @examples +#' \dontrun{ +#' categorical_raster |> +#' set_levels( +#' levs = tribble( +#' ~value, ~category, +#' 30, "URBAN CENTRE", +#' 23, "DENSE URBAN CLUSTER", +#' 22, "SEMI-DENSE URBAN CLUSTER", +#' 21, "SUBURBAN OR PERI-URBAN", +#' 13, "RURAL CLUSTER", +#' 12, "LOW DENSITY RURAL", +#' 11, "VERY LOW DENSITY RURAL", +#' 10, "WATER" +#' ) +#' ) %>% +#' as.data.frame() +#' } +#' +set_levels <- function(x, levs){ + + levels(x) <- levs + + x +} diff --git a/man/set_levels.Rd b/man/set_levels.Rd new file mode 100644 index 0000000..9fa6d61 --- /dev/null +++ b/man/set_levels.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/set_levels.R +\name{set_levels} +\alias{set_levels} +\title{set levels} +\usage{ +set_levels(x, levs) +} +\arguments{ +\item{x}{\code{SpatRaster}} + +\item{levs}{\code{data.frame}} +} +\value{ +\code{SpatRaster} +} +\description{ +Convenience function for setting \code{SpatRaster} levels names to enable setting in +piped workflows. +} +\examples{ +\dontrun{ +categorical_raster |> + set_levels( + levs = tribble( + ~value, ~category, + 30, "URBAN CENTRE", + 23, "DENSE URBAN CLUSTER", + 22, "SEMI-DENSE URBAN CLUSTER", + 21, "SUBURBAN OR PERI-URBAN", + 13, "RURAL CLUSTER", + 12, "LOW DENSITY RURAL", + 11, "VERY LOW DENSITY RURAL", + 10, "WATER" + ) + ) \%>\% + as.data.frame() +} + +}