R/read_file_to_tab.R

Defines functions read.file.to.tab

###############################
# file: path to table

#' @importFrom utils read.delim
read.file.to.tab <- function(file){
  
  if (grepl("\n", file)){
    file <- textConnection(file)
  } 
  tab <- read.delim(file, sep=";", 
                    header = FALSE, strip.white = TRUE, 
                    stringsAsFactors = FALSE, blank.lines.skip = TRUE, 
                    comment.char="#")
  
  
  # number of continuous variables:
  n.contin <- ncol(tab) - 3
  if (n.contin<1)
    stop ("Specification of continuous distributions missing in model file.")
  
  # remove spaces
  for (i in 1:ncol(tab)){
    tab[,i] <- gsub(" ","",tab[,i], fixed = TRUE)
  }
  if (any(tab == ""))
    stop ("Model file not completely specified (missing/empty entries)!")
  
  colnames(tab) <- c("tree", "cat", "mpt", paste0("c",1:n.contin))
  
  tab
}
danheck/gpt documentation built on Feb. 12, 2024, 6:21 a.m.