R/checks.R

Defines functions check_cmea is_levels_complete replaceNaCharacter any_double_quotes allDoubleQuotes has_agronomic_metadata

Documented in has_agronomic_metadata is_levels_complete replaceNaCharacter

#' Internal function to detect if data 
#' @param .data data retrieved from AgAPI
#' @description check anomalies in agronomic data after retrieving information from AGROFIMS database
#' @details Minimal conditions are 1 row, 2 columns, not all missing data
#' @importFrom checkmate checkDataFrame allMissing
#' @export
#'     
has_agronomic_metadata <- function(.data){
  
  n <- ncol(.data)
  .data <- .data[,-n]
  
  cond1 <- checkDataFrame( 
    x = .data,
    #types = character(0L),
    min.rows = 1L, #1 row at minimun
    min.cols = 2L  #2 cols at minimun
  )
  cond1 <- isTRUE(cond1)
  #if(isFALSE(cond1)){ return (FALSE)}
  #second condition: #check if all is complete
  cond2 <-  !all(sapply(.data, allMissing)) #not all is missing
  #cond3 <- !allDoubleQuotes(.data[,1]) #check if all are double quotes
  return(isTRUE(cond1 && cond2))
  
}

allDoubleQuotes <-  function(.x){
  if(length(.x)>0){
    identical(.x, "")   
  } else {
    FALSE
  }
}

any_double_quotes <- function(.x){
  
  if(length(.x)>0){
    return(any(stringr::str_detect(.x, "^$")==TRUE,na.rm = TRUE))     
  } else {
    return(FALSE)
  }
  
}

#' Replace NA character in data frames
#' @param .data data.frame to replace NA character by double quotes \code{("")}
#' @export
#' 
replaceNaCharacter <- function(.data){
  .data[is.na(.data)] <- ""
  .data
}

#' Detect if levels are missing
#' @param .data levels of experimental design factors
is_levels_complete <- function(.data){
  #If TRUE, all levels are complete, otherwise some are missing
  sum(match(x = .data[,"levelname"],""),na.rm = TRUE)==0 
}



check_cmea <- function(.data){

   cond<- nrow(traitlist %>% dplyr::filter(singularity=="crop_measurement"))>0
   
} 
AGROFIMS/ragrofims documentation built on Jan. 23, 2021, 8:40 a.m.