在knitr上设置全球千分离器 [英] Set global thousand separator on knitr

查看:156
本文介绍了在knitr上设置全球千分离器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  format(num,digits = 2,big.mark =,decimal.mark =,)

数字为2,小数点到逗号很容易,我只需要在我的第一个R块中设置 options(digits = 2,OutDec =,)。然而,我没有看到如何设置千位分隔符为(或者其他任何东西)。我也尝试调整 opts_chunk ,但不能得到它。



当然, m试图避免在每个输出,内联或其他方式中插入 format()。如何在knitr上设置默认的千位分隔符?

如何在knitr上设置默认的千位分隔符?

$ b $正如Frank指出的那样,设置一个像下面这样的knitr钩子解决了这个问题:

<$ p $ (x,big.mark =)
})

事实证明,knitr钩子是调整knitr上的R块的输出的好方法。看看 http://yihui.name/knitr/hooks 是非常值得的。 p>

来源: https://groups.google.com/forum/#!msg/knitr/CnFwvk1Qn1E/WY-Xhf7Ph3AJ


I want all the numbers on my knitr report to be formatted as such by default:

format(num, digits = 2, big.mark = " ", decimal.mark = ",")

Defaulting the number of digits to 2 and the decimal mark to comma is easy, I just need to set options(digits = 2, OutDec = ",") in my first R chunk. However, I don't see how I can set the thousand separator to " " (or anything else, for that matter) in that format. I've also tried tweaking opts_chunk, but can't get it to work.

Of course, I'm trying to avoid having to insert format() inside every output, inline or otherwise. More intelligent formatting is one thing that drove me towards knitr from Sweave, after all.

How can I set default thousand separator marks on knitr?

解决方案

As Frank noted, setting a knitr hook such as the following solves the problem:

knit_hooks$set(inline = function(x) {
  prettyNum(x, big.mark=" ")
})

It turns out knitr hooks are a great way to tweak the output of R chunks on knitr. It's really worth it to take a look at http://yihui.name/knitr/hooks.

Source: https://groups.google.com/forum/#!msg/knitr/CnFwvk1Qn1E/WY-Xhf7Ph3AJ

这篇关于在knitr上设置全球千分离器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆