knitr:无法用utf-8字符创建一个数字

时间:2016-09-05 22:01:34

标签: r encoding utf-8 knitr

以下是我的.Rnw文件:

\documentclass{article}
\begin{document}

<<myChunk>>=
options(warn = 2)
library(ggplot2)
library(directlabels)
data(BodyWeight,package="nlme")
BodyWeight$temp <- as.character(BodyWeight$Rat)
BodyWeight$temp[BodyWeight$temp == "4"] <- "HI₂"
p <- qplot(Time,weight,data=BodyWeight,colour=temp,geom="line")
direct.label(p,"first.qp")
@

\end{document}

以下是我如何从R中调用knitr:

library(knitr)
# I have tryied this but doesn't make difference:
# pdf.options(encoding='ISOLatin2.enc')
knit("mwe_knitr.Rnw")

我得到以下输出:

> knit("mwe_knitr.Rnw")


processing file: mwe_knitr.Rnw
  |......................                                           |  33%
  ordinary text without R code

  |...........................................                      |  67%
label: myChunk
Quitting from lines 5-13 (mwe_knitr.Rnw) 
Error in grid.Call(L_convert, x, as.integer(whatfrom), as.integer(whatto),  : 
  (converted from warning) conversion failure on 'HI₂' in 'mbcsToSbcs': dot substituted for <e2>

我尝试过使用编码的解决方案,例如发布在这里: Rhtml: Warning: conversion failure on '<var>' in 'mbcsToSbcs': dot substituted for <var>

(我在上面的评论中注意到我尝试解决该问题的确切位置),但它似乎对我没有任何改变。

我在Ubuntu上使用R 3.3.1和knitr package 1.13。

1 个答案:

答案 0 :(得分:3)

看起来使用cairo_pdf设备可以解决此问题。在下面的setup块中,我将设备选项设置为cairo_pdf设备(即开始option(device = ...的行),将全局块选项dev设置为默认为“cairo_pdf” “(在开始knitr::opts_chunk$set(...的行中)。 knitr documentation(参见“多字节字符编码”一节)和Issue #436中讨论了这种方法。

我做了一些其他改动:

  1. 而不是“硬编码”"HI₂"我已将Unicode符号用于下标2 "\U2082"

  2. 将绘图调用更改为“标准”ggplot而不是qplot。

  3. 在制作情节后调用directlabels更改为调用geom_dl以在“标准”ggplot工作流程中添加直接标签。

  4. fontfamily内设置geom_dl。我发现下标2是用一些字体系列渲染的,而不是其他字体系列。

  5. warn选项更改为零(默认值),以便警告不会变为错误。我在测试代码的时候就这样做了,但是如果需要,它当然可以设置为2.

  6. myChunk1a创建了图表。块myChunk1b创建基本相同的绘图,但在多个版本中,每个版本使用不同的字体系列。在这些版本中,您可以看到下标2是使用某些字体系列呈现的,而不是其他字体系列。我不确定是什么决定了这一点,结果可能会在您的系统上有所不同。

    \documentclass{article}
    \begin{document}
    
    <<setup, include=FALSE>>=
    options(warn = 0)
    options(device = function(file, width = 7, height = 7, ...) {
      cairo_pdf(tempfile(), width = width, height = height, ...)
    })
    knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE, dev="cairo_pdf")
    @
    
    <<myChunk>>=
    library(ggplot2)
    library(directlabels)
    library(gridExtra)
    library(dplyr)
    
    data(BodyWeight,package="nlme")
    BodyWeight$temp <- as.character(BodyWeight$Rat)
    
    BodyWeight$temp[BodyWeight$temp=="4"] = "HI\U2082"
    
    # Change first value so that HI2 label is easily visible
    BodyWeight$weight[BodyWeight$temp=="HI\U2082" & BodyWeight$Time==1] = 350
    @
    
    <<myChunk1a, fig.height=5>>=
    ggplot(BodyWeight, aes(Time, weight, colour=temp)) + 
      geom_line() +
      geom_dl(method=list("first.qp", fontfamily="Helvetica", cex=1), aes(label=temp)) + 
      theme_bw() +
      ggtitle("Helvetica") +
      guides(colour=FALSE)
    @
    
    <<myChunk1b, fig.height=11>>=
    # Create several plots, each demonstrating a different font family for the labels
    grid.arrange(grobs=lapply(c("Helvetica","Courier","Palatino","Times","Serif"), function(f) {
      ggplot(BodyWeight, aes(Time, weight, colour=temp)) + 
        geom_line() +
        geom_dl(method=list("first.qp", fontfamily=f, cex=1), aes(label=temp)) + 
        labs(x="") + 
        theme_bw() +
        theme(plot.margin=unit(c(0,0,0,0), "lines"),
              text=element_text(size=9)) +
        ggtitle(f) +
        guides(colour=FALSE)
    }), ncol=1)
    @
    
    <<myChunk2, fig.height=5>>=
    data(BodyWeight,package="nlme")
    BodyWeight$temp <- as.character(BodyWeight$Rat)
    
    # Change first value so that HI2 label is easily visible
    BodyWeight$weight[BodyWeight$temp=="4" & BodyWeight$Time==1] = 350
    
    # Set temp==4 to desired expression
    BodyWeight$temp[BodyWeight$temp == "4"] <- paste(expression(HI[2]))
    
    # Convert temp to factor to set order
    BodyWeight$temp = factor(BodyWeight$temp, levels=unique(BodyWeight$temp))
    
    qplot(Time, weight, data=BodyWeight, colour=temp, geom="line") +
      guides(colour=FALSE) +
      geom_text(data=BodyWeight %>% group_by(temp) %>%
                  filter(Time == min(Time)), 
                aes(label=temp, x=Time-0.5, y=weight), parse=TRUE, hjust=1) +
      theme_bw()
    @
    
    \end{document}
    

    以下是myChunk1a的情节:

    enter image description here