Skip to content

Instantly share code, notes, and snippets.

@brownag

brownag/munsell2rgb-SPC.R

Last active Jul 15, 2020
Embed
What would you like to do?
Example: A wrapper method (should be dispatched via S4) for munsell2rgb for the SoilProfileCollection object
# I just named this "munsell2rgb2"... but there is no reason why it cant be
# "munsell2rgb,SoilProfileCollection-method" in aqp
munsell2rgb2 <- function(object,
hue = "hue", value = "value", chroma = "chroma",
as.spc = TRUE) {
h <- horizons(object)
# makes a data.frame
drgb <- munsell2rgb(h[[hue]], h[[value]], h[[chroma]], returnLAB = TRUE)
idn <- idname(object)
hidn <- hzidname(object)
# munsell2rgb does not return ID names (not inherently aware of the SPC)
idcol <- data.frame(h[[idn]], h[[hidn]])
colnames(idcol) <- c(idn, hidn)
if(as.spc) {
# horizons<- will ensure merge.data.table triggers if @horizons is data.table
horizons(object) <- cbind(idcol, drgb)
return(object)
} else {
# TODO: remove :::
return(aqp:::.as.data.frame.aqp(cbind(idcol, drgb), aqp_df_class(object)))
}
}
library(aqp)
library(soilDB)
data(loafercreek)
# test with data.table
# loafercreek@horizons <- data.table::data.table(loafercreek@horizons)
# metadata(loafercreek)$aqp_df_class <- "data.table"
# test with tibble
# loafercreek@horizons <- tibble::as.tibble(loafercreek@horizons)
# metadata(loafercreek)$aqp_df_class <- "tbl_df"
# use SPC interface
res <- munsell2rgb2(loafercreek,
hue = "m_hue", value = "m_value", chroma = "m_chroma")
# inspect (its the class we set after join)
horizons(res)
# use data.frame-like interface (uses aqp_df_class)
res2 <- munsell2rgb2(loafercreek,
hue = "m_hue", value = "m_value", chroma = "m_chroma",
as.spc = FALSE)
res2
class(res2) # respects df class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.