r - svg 设备、表达式()和 unicode 字符的问题
问题描述
我在使用带有数学表达式和 unicode 的 svg 设备时遇到了困难(在这种情况下,是左引号和右引号)。这只发生在我的 Windows 10 计算机上;我的 osx 电脑似乎不受影响。考虑以下 R 代码:
output = tempfile(fileext = ".svg")
svg(file = output)
plot(1, axes=FALSE, ty='n')
## Line A:
mtext(side = 2, text = expression(paste("A")))
## Line B:
mtext(side = 3, text = expression(paste("“B")))
## Line C:
mtext(side = 4, text = expression(paste("C")))
axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
dev.off()
## Uncomment and run to open the SVG file to look at it
# browseURL(output)
(注意 B 行中的左引号)。在运行 C 线时,我得到
Error in mtext(side = 4, text = expression(paste("C"))) :
metric information not available for this device
- 如果我将设备更改为
png
我得到我期望的输出。 - 如果我注释掉 B 行和 C 行,我会得到我期望的输出(例如,A 行和轴的输出)
- 如果我只注释掉 C 行,我不会得到我期望的输出。B线和轴都不起作用。B 行似乎有问题,导致块中稍后的所有内容也失败。
- 如果我
expression()
从 B 行删除,一切都会按预期工作(包括 unicode 左引号) - 如果我使用更改字体
showtext
(有关详细信息,请参阅问题:https ://github.com/yihui/knitr/issues/1831 )我没有问题。
那么,这似乎是默认字体 , 和 unicode 之间的svg
交互expression
。但仅在 Windows 10 上。我的 OSX 计算机(请参阅下面的详细信息)没有问题。
sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reprex_0.3.0
loaded via a namespace (and not attached):
[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
[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
[15] processx_3.4.2 clipr_0.7.0 htmltools_0.4.0 knitr_1.28.3
OSX 会话信息(在这台电脑上没问题)
> xfun::session_info('knitr')
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4, RStudio 1.2.5031
Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8
Package version:
evaluate_0.14 glue_1.4.0 graphics_3.6.1 grDevices_3.6.1
highr_0.8 knitr_1.28.3 magrittr_1.5 markdown_1.1
methods_3.6.1 mime_0.9 stats_3.6.1 stringi_1.4.6
stringr_1.4.0 tools_3.6.1 utils_3.6.1 xfun_0.12
yaml_2.2.1
知道是什么原因造成的吗?
解决方案
我在我的 32 位 Windows 工作 PC 上遇到了同样的问题。这是一个棘手的错误,但我最终发现它在GMMathText
graphics.c的函数中被抛出。正如您所描述的,这似乎只是expression
s 在某些图形设备上包含某些字符的问题。特别是,在 Windows 上使用 Cairo 的任何东西似乎都在同一点上碰壁。
在尝试了几种技巧来解决这个问题后,我发现最好的解决方案是简单地使用bquote
而不是expression
. 这使您可以保留plotmath
获得的所有功能expression
而不会产生错误:
svg(output)
plot(1, axes=FALSE, ty='n')
mtext(side = 2, text = bquote("A"))
mtext(side = 3, text = bquote("“B"))
mtext(side = 4, text = bquote("C"))
axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
dev.off()
这给出了以下 SVG:
有一个很好的概述如何使用来生成你的标签,包括你需要的bquote
所有数学表达式
推荐阅读
- python - Python数据框HTML显示在jupyter之外
- javascript - 如何为我的幻灯片添加过渡滑动图像?
- r - R:plot3D scatter3D,有 coord_fixed() 选项吗?
- css - 插入符号不垂直居中,内容为空并显示 Flex
- java - Spring data jpa通过嵌入键中的多个字段查找
- angular - 从另一个组件路由时,角度材质输入未正确渲染
- mysql - MySQL:在 SELECT 语句中虚拟插入记录
- ssl - Scrapy 如何通过 SSL 代理抓取 HTTPS 站点
- laravel-5 - 如何在 Laravel 包中使用角色
- javascript - 在 React 服务器端渲染中使用 .dispatch 和箭头函数了解链式 .map 和 .filter