# see kpeaks documentation at https://cran.r-project.org/web/packages/kpeaks/kpeaks.pdf # publication at https://www.researchgate.net/publication/331258718_kpeaks_An_R_Package_for_Quick_Selection_of_K_for_Cluster_Analysis # you'll need to install jsonlite and kpeaks packages into Vertica R UDx as shown at https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/ExtendingVertica/R/RPackages.htm library("jsonlite") library("kpeaks") KPeaks_User <- function(input.data.frame, parameters.data.frame) { clusters <- findk(input.data.frame, binrule="bc", rcs=FALSE) dfSize <- nrow(input.data.frame) vector <- character() for (i in 0:dfSize) vector[i] <- toJSON(clusters) output <- data.frame(vector) return(output) } KPeaks_UserFactory <- function() { list(name = KPeaks_User, udxtype = c("scalar"), # Since this is a polymorphic function the intype must be any intype = c("any"), outtype = c("varchar(255)"), parametertypecallback=KPeaksParameters) } # I will likely extend this to support optional parameters to kpeaks like binrule KPeaksParameters <- function() { parameters <- list(datatype = c("varchar", "int"), length = c("NA", "NA"), scale = c("NA", "NA"), name = c("binrule", "nstart")) return(parameters) }