R/utf8.to.latin1.R

Defines functions utf8.to.latin1

Documented in utf8.to.latin1

#' @rdname latin1.to.utf8
#'
#' @export

utf8.to.latin1 <- function(file, force=FALSE)
{
  if(!isTRUE(file.encoding(file)=="UTF-8") && !force)
  {
    warning("could not verify that file is 'UTF-8' encoded, nothing done")
    return(invisible(NULL))
  }

  ## Remember original line endings
  ole <- line.endings(file)

  ## Convert file encoding UTF-8 -> latin1
  txt <- readLines(file, encoding="UTF-8")
  txt <- iconv(txt, from="UTF-8", to="latin1")
  writeLines(txt, file, useBytes=TRUE)

  ## Retain original line endings
  switch(ole, Dos=unix2dos(file), Unix=dos2unix(file))
}

Try the TAF package in your browser

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

TAF documentation built on March 31, 2023, 6:51 p.m.