Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use writeLines for fread(text=.) #4805

Merged
merged 1 commit into from
May 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/fread.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ yaml=FALSE, autostart=NA, tmpdir=tempdir(), tz="")
if (!is.character(text)) stop("'text=' is type ", typeof(text), " but must be character.")
if (!length(text)) return(data.table())
if (length(text) > 1L) {
cat(text, file=(tmpFile<-tempfile(tmpdir=tmpdir)), sep="\n") # avoid paste0() which could create a new very long single string in R's memory
writeLines(text, tmpFile<-tempfile(tmpdir=tmpdir)) # avoid paste0() which could create a new very long single string in R's memory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use useBytes=TRUE here to facilitate writing UTF-8 strings on Windows:

https://stackoverflow.com/q/10675360/3576984

Copy link

@mrdwab mrdwab Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelChirico This isn't something I've tested, but maybe fwrite(as.data.table(text), tmpFile<-tempfile(tmpdir = tmpdir), col.names = FALSE) is also worth exploring. I'm basing this assumption on the speed that I observed here.

Or maybe list(text) instead of as.data.table(text) since fwrite should work with that too..?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think it may warrant some benchmarking. Actually I have run into an issue recently where cat() is very slow for long input. In my case I was cat-ing a whole file by character (e.g. cat(strsplit(readChar(f, file.size(f)), NULL)[[1]], sep = '', file = f) is quite slow)

file = tmpFile
on.exit(unlink(tmpFile), add=TRUE)
} else {
Expand Down