-
Notifications
You must be signed in to change notification settings - Fork 18
/
_common.R
executable file
·100 lines (95 loc) · 2.74 KB
/
_common.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
knitr::knit_hooks$set(par = function(before, options, envir) {
if (before && options$fig.show != "none") {
par(
mar = c(4, 4, .5, .5)
)
}
})
knitr::opts_chunk$set(
comment = "#>"
)
if (grepl(x = sessionInfo()$running, pattern = "macOS")) {
# 准备 Noto 中英文字体
sysfonts::font_paths(new = "~/Library/Fonts/")
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJKsc-Regular.otf",
bold = "NotoSerifCJKsc-Bold.otf"
)
## 黑体
sysfonts::font_add(
family = "Noto Sans CJK SC",
regular = "NotoSansCJKsc-Regular.otf",
bold = "NotoSansCJKsc-Bold.otf"
)
} else if (grepl(x = sessionInfo()$running, pattern = "Fedora")) { # Github Action custom Docker Container Based on Fedora
sysfonts::font_paths(new = c(
"/usr/share/fonts/google-noto-serif-cjk-fonts" # Fedora 39
))
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJK-Regular.ttc",
bold = "NotoSerifCJK-Bold.ttc"
)
} else if (grepl(x = sessionInfo()$running, pattern = "Rocky")) {
sysfonts::font_paths(new = c(
"/usr/share/fonts/google-noto-cjk/" # Rocky Linux 9
))
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJK-Regular.ttc",
bold = "NotoSerifCJK-Bold.ttc"
)
## 黑体
sysfonts::font_add(
family = "Noto Sans CJK SC",
regular = "NotoSansCJK-Regular.ttc",
bold = "NotoSansCJK-Bold.ttc"
)
} else { # Ubuntu
sysfonts::font_paths(new = c(
"/usr/share/fonts/opentype/noto/",
"/usr/share/fonts/truetype/noto/"
))
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJK-Regular.ttc",
bold = "NotoSerifCJK-Bold.ttc"
)
## 黑体
sysfonts::font_add(
family = "Noto Sans CJK SC",
regular = "NotoSansCJK-Regular.ttc",
bold = "NotoSansCJK-Bold.ttc"
)
}
# 设置 Web GL 渲染
options(rgl.useNULL = TRUE)
options(rgl.printRglwidget = TRUE)
# tinytex 不要自动安装宏包
options(tinytex.install_packages = FALSE)
options(
tinytex.engine = "xelatex",
tikzDefaultEngine = "xetex",
tikzDocumentDeclaration = "\\documentclass[tikz]{standalone}\n",
tikzXelatexPackages = c(
"\\usepackage[fontset=fandol]{ctex}",
"\\usepackage{amsfonts,mathrsfs,amssymb}\n"
)
)
# 用 magick 将 pdf 格式图片转化为 png 格式
to_png <- function(fig_path) {
png_path <- sub("\\.pdf$", ".png", fig_path)
magick::image_write(magick::image_read_pdf(fig_path),
format = "png", path = png_path,
density = 300, quality = 100
)
return(png_path)
}
# 设置 Python
Sys.setenv(RETICULATE_PYTHON = "/opt/.virtualenvs/r-tensorflow/bin/python")
Sys.setenv(RETICULATE_PYTHON_ENV = "/opt/.virtualenvs/r-tensorflow")