Reading binary data of indeterminate length in R
-
22-09-2019 - |
Question
I would like to read a binary file -- of indeterminate length -- directly from a URL in R. Using readBin
to read from a URL, without specifying the file size, does not work.
anImage <- readBin('http://user2010.org/pics/useR-large.png','raw')
Is there another approach that would allow this?
Solution
This will download the file to the working directory, but not directly into memory.
download.file('http://user2010.org/pics/useR-large.png', 'anImage.png')
The Rcurl package may also do what you want. (link not posted because of SO restrictions)
OTHER TIPS
A simple solution if to set 'n' to be reasonably large, read the file, check for possible overflow, and try again if necessary.
N <- 1e7
repeat
{
anImage <- readBin(filename, 'raw', n=N)
if(length(anImage) == N) N <- 5 * N else break
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow