打印xtable gsub语言环境问题

时间:2014-09-07 17:25:10

标签: r xtable

我有一个数据表,其中一些字段从word文档中复制和粘贴。当我尝试从RMarkdown输出xtable打印时,会出现此错误:

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

这是一个可重复的例子。我打电话给这个数据帧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")

即使我有像这样奇怪的字符,我怎样才能打印xtable?

仅供参考,当我在Windows上运行时,相同的操作无误地运行。 在debian linux上,我收到错误。我也检查了我的语言环境,并且设置正确。

1 个答案:

答案 0 :(得分:3)

好吧,如果从Windows复制数据,编码很可能是“latin1”。我猜测debian linux的默认编码是“UTF-8”。现在,当你说你正在复制数据时,我并不清楚你是如何将它变成R的,但听起来这些字节没有被转换为正确的编码。

鉴于您的示例data.frame,您可以通过明确“Short.Description”字段的编码来“修复”错误(在这种情况下触发错误)。尝试

 Encoding(test4$Short.Description) <- "latin1"

然后,如果你再次运行print(),你应该得到这样的东西。

<!-- 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>