Created
February 4, 2022 05:59
-
-
Save poliquin/5066f0b7703ba2167cd49c11ce6c340e to your computer and use it in GitHub Desktop.
Read text file with NULL bytes in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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