Skip to content

Instantly share code, notes, and snippets.

@poliquin
Created February 4, 2022 05:59
Show Gist options
  • Save poliquin/5066f0b7703ba2167cd49c11ce6c340e to your computer and use it in GitHub Desktop.
Save poliquin/5066f0b7703ba2167cd49c11ce6c340e to your computer and use it in GitHub Desktop.
Read text file with NULL bytes in R
read_delim_nul <- function(path, encoding = 'utf8', ...) {
#' Read a delimited text file that contains NULL bytes.
#'
#' @param path One or more file paths.
#' @param encoding File encoding.
#' @param ... Arguments to read_delim.
# can process multiple file paths
if (length(path) > 1) {
return(
Reduce(
function(...) rbind(...),
lapply(path, read_delim_nul, encoding = encoding, ...)
)
)
}
data <- readr::read_lines_raw(path)
# convert raw bytes to strings, ignoring NULL
data <- sapply(
data,
function(x) sys::as_text(x, encoding = encoding, skipNul = T, warn = F)
)
data <- as.vector(data, mode = 'character')
# now parse the data without NULLs
readr::read_delim(I(data), ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment