带有 knitr 和 Rmarkdown 的 Unicode [英] Unicode with knitr and Rmarkdown

查看:34
本文介绍了带有 knitr 和 Rmarkdown 的 Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一组在 knitr 和 Rmarkdown 中使用 Unicode 的最佳实践或文档?编织文档时,我似乎无法正确显示任何字形.

Is there a set of best practices or documentation for working with Unicode in knitr and Rmarkdown? I can't seem to get any glyphs to show up properly when knitting a document.

例如,这适用于控制台(在 Rstudio 中):

For example, this works in the console (in Rstudio):

> cat("U2660   U2665  U2666  U2663")
♠   ♥   ♦   ♣

但是在编织时我得到了这个:

But when knitting I get this:

  • HTML

推荐答案

看起来像是 Windows 特有的编码问题,可能与此问题有关:https://github.com/hadley/evaluate/issues/59 不幸的是,我们必须等待基础 R 中的修复,但如果您没有使用 cat(),并且这个表达式是代码块中的 顶级 表达式(例如不在 for 循环或 if 语句中),我猜这个可能工作:

It looks like an encoding issue specific to Windows, and may be related to this issue: https://github.com/hadley/evaluate/issues/59 Unfortunately we have to wait for a fix in base R, but if you don't have to use cat(), and this expression is a top-level expression in your code chunk (e.g. not inside a for-loop or if-statement), I guess this may work:

knitr::asis_output("U2660   U2665  U2666  U2663")

它直接将字符串传递给 knitr 并绕过 cat(),因为 knitr 不能可靠地捕捉由 cat() 在 Windows 上 - 这取决于字符是否可以由系统的本机编码表示.

It passes the character string directly to knitr and bypasses cat(), since knitr cannot reliably catch multibyte characters written out by cat() on Windows -- it depends on whether the characters can be represented by your system's native encoding.

这篇关于带有 knitr 和 Rmarkdown 的 Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆