如何将randomly-generated数据传递给R/exams中的include_tikz?
How to pass randomly-generated data to include_tikz in R/exams?
当我将静态数据添加到使用 TikZ 创建的图表和 R/exams 中的 include_tikz()
时,它们会正确呈现。例如我创建了下面的图形
使用 exams::include_tikz(graf01, library = "arrows")
和下面的 TikZ 代码。然后我可以将它包含在 exams2html()
、exams2moodle()
等
中
graf01<-c("\definecolor{uuuuuu}{rgb}{0.26,0.26,0.26}",
"\definecolor{zzttqq}{rgb}{0.6,0.2,0}",
"\definecolor{ududff}{rgb}{0.30,0.30,1}",
"\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]",
"\clip(-0.1,-0.1) rectangle (5,5);",
"\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10] (0,0) -- (4.68,-0.02) -- (4.7,4.66) -- (0.02,4.68) -- cycle;",
"\draw (3.44,3.80) node[anchor=north west] {\textbf{12}};",
"\draw (0.35,4.57) node[anchor=north west] {\textbf{DH}};",
"\draw (3.97,4.57) node[anchor=north west] {\textbf{CA}};",
"\draw (2.31,0.72) node[anchor=north west] {\textbf{R}};",
"\draw [line width=2pt] (1.76,3.08) circle (1.3cm);",
"\draw [line width=2pt] (3.05,3.13) circle (1.3cm);",
"\draw [line width=2pt] (2.45,1.98) circle (1.3cm);",
"\draw [line width=2pt,color=zzttqq] (0,0)-- (4.68,-0.02);",
"\draw [line width=2pt,color=zzttqq] (4.68,-0.02)-- (4.7,4.66);",
"\draw [line width=2pt,color=zzttqq] (4.7,4.66)-- (0.02,4.68);",
"\draw [line width=2pt,color=zzttqq] (0.02,4.68)-- (0,0);",
"\begin{scriptsize}",
"\draw [fill=ududff] (0,0) circle (2.5pt);",
"\draw [fill=ududff] (4.68,-0.02) circle (2.5pt);",
"\draw [fill=uuuuuu] (4.7,4.66) circle (2.5pt);",
"\draw [fill=uuuuuu] (0.02,4.68) circle (2.5pt);",
"\end{scriptsize}",
"\end{tikzpicture}")
现在 graf01[7]
中的数字 12 而不是 hard-coding
"\draw (3.44,3.80) node[anchor=north west] {\textbf{12}};"
我想用一个randomly-generated号码,比如:
vald <- sample(1:20, 1)
我试过使用
"\draw (3.44,3.80) node[anchor=north west] {\textbf{vald}};"
但随后包含字符串 vald
而不是其值:
如何包含 vald
的值?
要在字符串中包含变量的值,您可以使用函数 paste()
或 paste0()
:
vald <- 7
line7 <- paste0("\draw (3.44,3.80) node[anchor=north west] {\textbf{", vald, "}};")
line7
## [1] "\draw (3.44,3.80) node[anchor=north west] {\textbf{7}};"
或等效输出sprintf()
:
line7 <- sprintf("\draw (3.44,3.80) node[anchor=north west] {\textbf{%s}};", vald)
如果字符串中有多个占位符,例如 %s
,或者当您有不同类型的占位符等时,后者通常更方便。
无论哪种情况,您随后都可以执行以下操作:
graf01[7] <- line7
include_tikz(graf01, library = "arrows")
当我将静态数据添加到使用 TikZ 创建的图表和 R/exams 中的 include_tikz()
时,它们会正确呈现。例如我创建了下面的图形
使用 exams::include_tikz(graf01, library = "arrows")
和下面的 TikZ 代码。然后我可以将它包含在 exams2html()
、exams2moodle()
等
graf01<-c("\definecolor{uuuuuu}{rgb}{0.26,0.26,0.26}",
"\definecolor{zzttqq}{rgb}{0.6,0.2,0}",
"\definecolor{ududff}{rgb}{0.30,0.30,1}",
"\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]",
"\clip(-0.1,-0.1) rectangle (5,5);",
"\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10] (0,0) -- (4.68,-0.02) -- (4.7,4.66) -- (0.02,4.68) -- cycle;",
"\draw (3.44,3.80) node[anchor=north west] {\textbf{12}};",
"\draw (0.35,4.57) node[anchor=north west] {\textbf{DH}};",
"\draw (3.97,4.57) node[anchor=north west] {\textbf{CA}};",
"\draw (2.31,0.72) node[anchor=north west] {\textbf{R}};",
"\draw [line width=2pt] (1.76,3.08) circle (1.3cm);",
"\draw [line width=2pt] (3.05,3.13) circle (1.3cm);",
"\draw [line width=2pt] (2.45,1.98) circle (1.3cm);",
"\draw [line width=2pt,color=zzttqq] (0,0)-- (4.68,-0.02);",
"\draw [line width=2pt,color=zzttqq] (4.68,-0.02)-- (4.7,4.66);",
"\draw [line width=2pt,color=zzttqq] (4.7,4.66)-- (0.02,4.68);",
"\draw [line width=2pt,color=zzttqq] (0.02,4.68)-- (0,0);",
"\begin{scriptsize}",
"\draw [fill=ududff] (0,0) circle (2.5pt);",
"\draw [fill=ududff] (4.68,-0.02) circle (2.5pt);",
"\draw [fill=uuuuuu] (4.7,4.66) circle (2.5pt);",
"\draw [fill=uuuuuu] (0.02,4.68) circle (2.5pt);",
"\end{scriptsize}",
"\end{tikzpicture}")
现在 graf01[7]
"\draw (3.44,3.80) node[anchor=north west] {\textbf{12}};"
我想用一个randomly-generated号码,比如:
vald <- sample(1:20, 1)
我试过使用
"\draw (3.44,3.80) node[anchor=north west] {\textbf{vald}};"
但随后包含字符串 vald
而不是其值:
如何包含 vald
的值?
要在字符串中包含变量的值,您可以使用函数 paste()
或 paste0()
:
vald <- 7
line7 <- paste0("\draw (3.44,3.80) node[anchor=north west] {\textbf{", vald, "}};")
line7
## [1] "\draw (3.44,3.80) node[anchor=north west] {\textbf{7}};"
或等效输出sprintf()
:
line7 <- sprintf("\draw (3.44,3.80) node[anchor=north west] {\textbf{%s}};", vald)
如果字符串中有多个占位符,例如 %s
,或者当您有不同类型的占位符等时,后者通常更方便。
无论哪种情况,您随后都可以执行以下操作:
graf01[7] <- line7
include_tikz(graf01, library = "arrows")