svg 设备、表达式() 和 unicode 字符的问题

ric*_*rey 6 unicode svg r

我在使用带有数学表达式和 unicode 的 svg 设备时遇到困难(在本例中为左引号和右引号)。这只发生在我的 Windows 10 计算机上;我的 osx 计算机似乎不受影响。考虑以下 R 代码:

\n\n
output = tempfile(fileext = ".svg")\n\nsvg(file = output)\n\nplot(1, axes=FALSE, ty=\'n\')\n\n## Line A: \nmtext(side = 2, text = expression(paste("A")))\n## Line B:\nmtext(side = 3, text = expression(paste("\xe2\x80\x9cB")))\n## Line C: \nmtext(side = 4, text = expression(paste("C")))\n\naxis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)\n\ndev.off()\n\n## Uncomment and run to open the SVG file to look at it \n# browseURL(output)\n
Run Code Online (Sandbox Code Playgroud)\n\n

(注意 B 行中的左引号)。运行C线后,我得到

\n\n
Error in mtext(side = 4, text = expression(paste("C"))) : \n  metric information not available for this device\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • 如果我将设备更改为png我会得到我期望的输出。
  • \n
  • 如果我注释掉 B 行和 C 行,我会得到我期望的输出(例如,A 行和轴的输出)
  • \n
  • 如果我只注释掉 C 行,我就不会得到我期望的输出。B 线和轴都不起作用。B 行似乎有问题,导致块中后面的所有内容也失败。
  • \n
  • 如果我从 B 行删除expression(),一切都会按预期工作(包括 unicode 左引号)
  • \n
  • 如果我使用更改字体showtext(有关详细信息,请参阅问题:https://github.com/yihui/knitr/issues/1831)我没有问题。
  • \n
\n\n

那么,这似乎是默认字体 、 和 unicode之间svg的交互。expression但仅限于 Windows 10。我的 OSX 计算机(请参阅下面的详细信息)没有此问题。

\n\n
sessionInfo()\nR version 3.6.2 (2019-12-12)\nPlatform: x86_64-w64-mingw32/x64 (64-bit)\nRunning under: Windows 10 x64 (build 18363)\n\nMatrix products: default\n\nlocale:\n[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   \n[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          \n[5] LC_TIME=English_United States.1252    \n\nattached base packages:\n[1] stats     graphics  grDevices utils     datasets  methods   base     \n\nother attached packages:\n[1] reprex_0.3.0\n\nloaded via a namespace (and not attached):\n [1] Rcpp_1.0.4.6    ps_1.3.2        digest_0.6.25   R6_2.4.1        evaluate_0.14   rlang_0.4.5     fs_1.4.1       \n [8] callr_3.4.3     whisker_0.4     rmarkdown_2.1   Cairo_1.5-12    tools_3.6.2     xfun_0.13       compiler_3.6.2 \n[15] processx_3.4.2  clipr_0.7.0     htmltools_0.4.0 knitr_1.28.3   \n
Run Code Online (Sandbox Code Playgroud)\n\n

OSX 会话信息(在此计算机上没有问题)

\n\n
> xfun::session_info(\'knitr\')\nR version 3.6.1 (2019-07-05)\nPlatform: x86_64-apple-darwin15.6.0 (64-bit)\nRunning under: macOS Catalina 10.15.4, RStudio 1.2.5031\n\nLocale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8\n\nPackage version:\n  evaluate_0.14   glue_1.4.0      graphics_3.6.1  grDevices_3.6.1\n  highr_0.8       knitr_1.28.3    magrittr_1.5    markdown_1.1   \n  methods_3.6.1   mime_0.9        stats_3.6.1     stringi_1.4.6  \n  stringr_1.4.0   tools_3.6.1     utils_3.6.1     xfun_0.12      \n  yaml_2.2.1 \n
Run Code Online (Sandbox Code Playgroud)\n\n

知道是什么原因造成的吗?

\n

JGN*_*GNI 0

我在这里进行一些猜测,因为我对 R 一无所知。但是,您的 Windows 盒子的本地版本是 cp1252,它不包含 Unicode 引号,但使用 Microsoft 的智能引号。

你说它在 OS-X 中工作,它查看你的会话信息设置为使用 UTF-8。

我假设您的程序文件保存为 UTF-8。