【发布时间】:2013-09-04 07:24:14
【问题描述】:
使用knitr,我想打印一些变量的几个函数的结果,例如
There were x1 (a=\`r a(x1)\`; b=\`r b(x1)\`) and x2 (a=\`r a(x2)\`; b=\`r b(x2)\`)
因为我不喜欢对每个变量都重复这个,所以我写了一个函数来生成字符串:
foo <- function(x) paste('a = ',a(x), '; b = ', b(x))
现在我可以写There were x1 (`r foo(x1)`) and x2 (`r foo(x2)`)。
它可以工作,但不使用options('digits'),所以它打印15位数字。 cat() 使用 options('digits'),但显然是 using cat is discouraged 并且在使用内联代码时不输出任何内容。
是否有替代 paste() 的替代方法,它使用 `options('digits') 格式化数字并在内联块中创建输出?
【问题讨论】: