#' Add a type in subplot table #' #' Add feature and associated descriptors in subplot list table #' #' @return nothing #' #' @author Gilles Dauby, gilles.dauby@ird.fr #' @param new_type string value with new type descritors - try to avoid space #' @param new_valuetype string one of following 'numeric', 'integer', 'categorical', 'ordinal', 'logical', 'character' #' @param new_maxallowedvalue numeric if valuetype is numeric, indicate the maximum allowed value #' @param new_minallowedvalue numeric if valuetype is numeric, indicate the minimum allowed value #' @param new_typedescription string full description of trait #' @param new_factorlevels string a vector of all possible value if valuetype is categorical or ordinal #' @param new_expectedunit string expected unit (unitless if none) #' @param new_comments string any comments #' #' @export add_subplottype <- function(new_type = NULL, new_valuetype = NULL, new_maxallowedvalue = NULL, new_minallowedvalue = NULL, new_typedescription = NULL, new_factorlevels = NULL, new_expectedunit = NULL, new_comments = NULL) if(is.null(new_type)) stop("define new type") if(try_open_postgres_table(table = "subplotype_list", con = mydb) dplyr::distinct(type) dplyr::filter(type == !!new_type) dplyr::collect() nrow()>0) stop("new type already in table") if (is.null(new_valuetype)) stop("define new_valuetype") if (!any(new_valuetype==c('numeric', 'integer', 'categorical', 'ordinal', 'logical', 'character', 'table_colnam'))) stop("valuetype should one of following 'numeric', 'integer', 'categorical', 'ordinal', 'logical', 'character' or 'table_colnam'") if (new_valuetype=="numeric" | new_valuetype=="integer") if (!is.numeric(new_maxallowedvalue) & !is.integer(new_maxallowedvalue)) stop("valuetype numeric of integer and max value not of this type") if (new_valuetype=="numeric" | new_valuetype=="integer") if (!is.numeric(new_minallowedvalue) & !is.integer(new_minallowedvalue)) stop("valuetype numeric of integer and min value not of this type") mydb <- call.mydb() new_data_renamed <- tibble(type = new_type, valuetype = new_valuetype, maxallowedvalue = ifelse(is.null(new_maxallowedvalue), NA, new_maxallowedvalue), minallowedvalue = ifelse(is.null(new_minallowedvalue), NA, new_minallowedvalue), typedescription = ifelse(is.null(new_typedescription), NA, new_typedescription), factorlevels = ifelse(is.null(new_factorlevels), NA, new_factorlevels), expectedunit = ifelse(is.null(new_expectedunit), NA, new_expectedunit), comments = ifelse(is.null(new_comments), NA, new_comments)) print(new_data_renamed) Q <- choose_prompt(message = "confirm adding this type?") if(Q) DBI::dbWriteTable(mydb, "subplotype_list", new_data_renamed, append = TRUE, row.names = FALSE)