R/write.R

Defines functions writeAllColRow

Documented in writeAllColRow

#' @title Write ID objects into a text file that can be used by GLOBIOM
#' @description Write 10 ID objects per line, plus a header and a line with slash,
#' according to GAMS requisites.
#' @param tokens A vector of IDs.
#' @param header A string.
#' @param outfile Name of the file to be saved.
#' @export
writeAllColRow <- function(tokens, header, outfile){
  if(length(unique(tokens)) < length(tokens)){
    warning("Removing repeated IDs before saving them")
    tokens <- unique(tokens)
  }

  result <- paste0(header, "\n/\n")
  nlines <- ceiling(length(tokens) / 10)
  token.list <- split(tokens, rep(1:nlines,
                                  each=10, len=length(tokens)))
  tokens <- paste(lapply(token.list,paste,collapse=", "), collapse = ",\n")
  result <- paste0(result, tokens)

  f <- file(outfile)
  write(result, f)
  close(f)
}
pedro-andrade-inpe/colrow documentation built on Oct. 3, 2023, 8:48 a.m.