针织钩可分隔000英寸,但不能分隔多年 [英] knitr hook to separate 000's, but not for years
问题描述
我在 rnw
的顶部定义一个钩子,以逗号分隔'000':
knit_hooks $ set(inline = function(x){prettyNum(x,big.mark =,")})
但是,有些数字我不想这样格式化,例如年份.在下面的示例中,当我打印 \ Sexpr {nocomma}
时,是否有更好的写钩子的方法,或者重写钩子的方法?
\ documentclass {article}\ begin {document}<< setup>> =图书馆(针织)options(scipen = 999)#关闭数字的科学记数法opts_chunk $ set(echo = FALSE,警告= FALSE,消息= FALSE)knit_hooks $ set(inline = function(x){prettyNum(x,big.mark =,")})Wantcomma<-1234 * 5nocomma<-"2014年9月1日"@该钩子将\ Sexpr {wantcomma}和\ Sexpr {nocomma}分开,但是我不想分开年份.\ end {document}
输出:
该钩子将分隔6,170和9月1日2,014,但是我不想分隔年份.
如果您不希望用逗号分隔的唯一内容是使用了多年的 strings ,请使用:
knit_hooks $ set(inline = function(x){if(is.numeric(x)){return(prettyNum(x,big.mark =,"))}别的{返回(x)}})
适用于您的日历字符串.但是,假设您只想自己打印年份数字?好吧,如何使用上面的钩子并将其转换为字符:
\ Sexpr {2014}呢?%感到困惑\ Sexpr {as.character(2014)}呢?%不感到惊讶
或可能(未试用):
\ Sexpr {paste(2014)}呢?%不感到惊讶
它将标量转换为字符,并节省了一些键入操作.虽然我们在这里没有打高尔夫……
或者是基于类的方法:
逗号<-函数(x){structure(x,class =逗号")}nocomma<-函数(x){structure(x,class ="nocomma")}options(scipen = 999)#关闭数字的科学记数法opts_chunk $ set(echo = FALSE,警告= FALSE,消息= FALSE)knit_hooks $ set(inline = function(x){if(inherits(x,逗号"))return(prettyNum(x,big.mark =,"))if(inherits(x,"nocomma"))return(x)return(x)#默认})Wantcomma<-1234 * 5nocomma1<-"2014年9月1日"#此处更改注释名称以不与功能冲突
然后只需将您的 Sexpr
包装在逗号
或 nocomma
中,例如:
挂钩将\ Sexpr {逗号(wantcomma)}和\ Sexpr {nocomma(nocomma1)}分开,但是我不想分开年份.
如果您想让默认值逗号化,则将注释为#default"的行更改为使用 prettyNum
.尽管我在想,我对此过于复杂了,并且逗号
和 nocomma
函数只可以自己计算字符串格式,然后就完全不需要钩子了./p>
在不确切知道您的案件的情况下,我认为我们无法编写一个推断逗号分隔方案的函数-例如,它必须知道"2013年的1342个案件"需要第一个数字而不是第二个数字...
I define a hook at the top of my rnw
to separate '000s with commas:
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
However, there are some numbers that I don't want to format like this, such as years. Is there a better way to write the hook, or a way to override the hook when I print \Sexpr{nocomma}
in the example below?
\documentclass{article}
\begin{document}
<<setup>>=
library(knitr)
options(scipen=999) # turn off scientific notation for numbers
opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
wantcomma <- 1234*5
nocomma <- "September 1, 2014"
@
The hook will separate \Sexpr{wantcomma} and \Sexpr{nocomma}, but I don't want to separate years.
\end{document}
Output:
The hook will separate 6,170 and September 1, 2,014, but I don’t want to separate years.
If the only things your don't want comma-separated are strings that have years in, use:
knit_hooks$set(inline = function(x) {
if(is.numeric(x)){
return(prettyNum(x, big.mark=","))
}else{
return(x)
}
})
That works for your calendar string. But suppose you want to just print a year number on its own? Well, how about using the above hook and converting to character:
What about \Sexpr{2014}? % gets commad
What about \Sexpr{as.character(2014)}? % not commad
or possibly (untested):
What about \Sexpr{paste(2014)}? % not commad
which converts the scalar to character and saves a bit of typing. We're not playing code golf here though...
Alternatively a class-based method:
comma <- function(x){structure(x,class="comma")}
nocomma <- function(x){structure(x,class="nocomma")}
options(scipen=999) # turn off scientific notation for numbers
opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
knit_hooks$set(inline = function(x) {
if(inherits(x,"comma")) return(prettyNum(x, big.mark=","))
if(inherits(x,"nocomma")) return(x)
return(x) # default
})
wantcomma <- 1234*5
nocomma1 <- "September 1, 2014" # note name change here to not clash with function
Then just wrap your Sexpr
in either comma
or nocomma
like:
The hook will separate \Sexpr{comma(wantcomma)} and \Sexpr{nocomma(nocomma1)}, but I don't want to separate years.
If you want the default to commaify then change the line commented "# default" to use prettyNum
. Although I'm thinking I've overcomplicated this and the comma
and nocomma
functions could just compute the string format themselves and then you wouldn't need a hook at all.
Without knowing exactly your cases I don't think we can write a function that infers the comma-sep scheme - for example it would have to know that "1342 cases in 2013" needs its first number commad and not its second...
这篇关于针织钩可分隔000英寸,但不能分隔多年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!