R/utils.R

Defines functions make_file_numbers make_batches assessment_ids

assessment_ids <- function(data, in_id){
  setdiff(1:nrow(data), in_id)
}

make_batches <- function(split_me, batch_size){
  if(is.data.frame(split_me)){
    n <- nrow(split_me)
  }
  else if(is.vector(split_me)){
    n <- length(split_me)
  }
  else{
    stop("Can only split dataframes or vectors.")
  }
  
  split(split_me, rep(1:ceiling(n/batch_size), each = batch_size)[1:n])
}


make_file_numbers <- function(overwrite, out_folder, file_prefix, n){
  if(overwrite){
    new_file_numbers <- 1:n
  }else{
    existing_file_numbers <-
      readr::parse_number(list.files(out_folder, pattern = paste0("^", file_prefix, ".*\\.RDS")))
    
    max_file_number <- if(length(existing_file_numbers) > 0) max(existing_file_numbers) else 0
    new_file_numbers <-  max_file_number + (1:n)
  }
  new_file_numbers
}
artichaud1/tidygrid documentation built on July 6, 2018, 9:10 a.m.