I have a data table where some fields are copied and pasted from word documents. When I attempt to output xtable prints from RMarkdown, it get this error:
Error in gsub("&", "&", result, fixed = TRUE) :
input string 3 is invalid in this locale
Calls: <Anonymous> ... eval -> eval -> print -> print.xtable -> sanitize -> gsub
Execution halted
Here's a reproducible example. I called this dataframe test4:
library(xtable)
test4 <- structure(list(Record.ID = 81, Record.Type = "Type1", Short.Description = "specify 2-8\xb0C storage location",Record.State = "Work in Progress", Owner = "person1", Due.Date = "2014-08-14",days.left = -24), row.names = c(NA, -1L), .Names = c("Record.ID","Record.Type","Short.Description", "Record.State", "Owner","Due.Date", "days.left"), class = "data.frame")
print(xtable(test4,display=c("d","d","s","s","s","s","s","d")),include.rownames=F,floating=F,type="html")
How can I get xtable to print even though I will have weird characters like this?
FYI, the same operation works without errors when I run it on windows. On debian linux, I get the error. Also I checked my locale and it is set correctly.
Well, if you are copying data from Windows, the encoding is most likely "latin1". I'm guessing the default encoding for debian linux is "UTF-8". Now when you are saying you are copying data, it's not clear to me exactly how you are getting this into R, but it sounds like the bytes aren't being converted to the correct encoding.
Given your example data.frame, you can "fix" the error by being explicit about the encoding of the "Short.Description" field (which is what is triggering the error in this case). Try
Encoding(test4$Short.Description) <- "latin1"
Then, if you run the print()
again, you should get something like this.
<!-- html table generated in R 3.1.0 by xtable 1.7-3 package -->
<!-- Sun Sep 7 13:33:56 2014 -->
<TABLE border=1>
<TR> <TH> Record.ID </TH> <TH> Record.Type </TH> <TH> Short.Description </TH>
<TH> Record.State </TH> <TH> Owner </TH> <TH> Due.Date </TH>
<TH> days.left </TH> </TR>
<TR> <TD align="right"> 81 </TD> <TD> Type1 </TD>
<TD> specify 2-8°C storage location </TD>
<TD> Work in Progress </TD> <TD> person1 </TD> <TD> 2014-08-14 </TD>
<TD align="right"> -24 </TD> </TR> </TABLE>