编织钩可分开000,但不能使用多年 [英] knitr hook to separate 000's, but not for years

查看:68
本文介绍了编织钩可分开000,但不能使用多年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在rnw的顶部定义了一个钩子,以逗号分隔'000':

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=",")
    })

但是,有些数字我不想这样格式化,例如年份.在下面的示例中,当我打印\Sexpr{nocomma}时,是否有更好的写钩子的方法,或者重写钩子的方法?

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}

输出:

该钩子将分隔6,170和9月1日2,014,但是我不想分隔年份.

The hook will separate 6,170 and September 1, 2,014, but I don’t want to separate years.

推荐答案

如果您不希望用逗号分隔的唯一内容是使用了多年的 strings ,请使用:

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

或可能(未试用):

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...

或者是基于类的方法:

  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

然后将您的Sexpr包裹在commanocomma中,例如:

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.

如果要使默认值逗号化,则将注释为#default"的行更改为使用prettyNum.尽管我认为我对此过于复杂,并且commanocomma函数可以自己计算字符串格式,然后您根本不需要钩子.

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.

在不确切知道您的案件的情况下,我认为我们无法编写一个推断逗号分隔方案的函数-例如,它必须知道"2013年的1342个案件"需要第一个逗号而不是第二个数字. ..

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屋!

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