R/getDelimiter.R

Defines functions `getDelimiter`

#' @description Guess the column delimiter from a text file
#' @return Character scalar
#' @noRd
`getDelimiter` <- function(x) {
    
    delimiter <- ","
        
    csvreadfile <- read.csv(x, as.is = TRUE)
    
    # if the delimiter is not a comma, there will be only one big column
    if (ncol(csvreadfile) == 1) { # try ";" separated
        delimiter <- ";"
        csvreadfile <- read.csv(x, sep = ";", as.is = TRUE)
    }
    
    # if still the delimiter is not the right one
    if (ncol(csvreadfile) == 1) { # try tab separated
        delimiter <- "\t"
        csvreadfile <- read.csv(x, sep = "\t", as.is = TRUE)
    }
    
    # finally, if it's still not the right delimiter stop and print an error message
    if (ncol(csvreadfile) == 1) {
        return("unknown")
    }
    
    return(delimiter)
        
}

Try the DDIwR package in your browser

Any scripts or data that you put into this service are public.

DDIwR documentation built on Oct. 1, 2023, 5:06 p.m.