Use readBin: readBin('/path/to/file', 'raw', n=1000000)
Here is an example of reading in a JPEG file: > x <- readBin(fileName,'raw',n=1000000) > str(x) raw [1:801403] ff d8 ff e1 ... > # convert to a HEX string (on the first 20 bytes) > paste(sprintf("%s", x[1:20]), collapse='') [1] "ffd8ffe1fffe4578696600004d4d002a00000008" > On Sun, May 16, 2010 at 11:13 AM, Dennis Fisher <fis...@plessthan.com>wrote: > Colleagues, > > I am using R to assemble RTF documents (which are plain text). I need to > embed a JPEG graphic that was created with R. I presume that the steps need > to be: > a. read the file into R > b. convert the object to HEX format > c. write the converted object to a textfile. > > If I read the file into R using readLines, I get the following (only the > first 5 lines shown): > > > readLines("/path/to/file") > > [1] "\xff\xd8\xff\xe0" > > [2] "\002\xa3\003\001\"" > > [3] "\v\xff\xc4" > > [4] > "\026\027\030\031\032%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4" > > [5] "\v\xff\xc4" > > > and I also receive a number of warning messages: > > Warning messages: > > 1: In encodeString(object, quote = "\"", na.encode = FALSE) : > > it is not known that wchar_t is Unicode on this platform > > I assume (naively) that I need some other approach to reading the file. I > also presume (again, naively) that once I have read the file successfully, I > can convert the contents to hex format. However, it is not obvious to me > what approach should be used to read the file. I found the command > read.jpeg (in rimage; of note, I needed to use the Windows version because > the OSX version appears to be broken). However, this command creates an > "imagematrix", which does not appear to be what I need. > > Any thoughts? > > Thanks in advance. > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-866-PLessThan (1-866-753-7784) > www.PLessThan.com <http://www.plessthan.com/> > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.