alter default display behavior for inline expressions

1,706 views
Skip to first unread message

Carl Boettiger

unread,
Apr 9, 2012, 3:46:10 PM4/9/12
to knitr
Hi list,

Is there a way to set some option such that in-line expressions are always wrapped with I() and prettyNum?

For instance, it is nice to include results in the final text that involve the numerical output of some R call
without making it obvious that they come from code (i.e. numbers that have commas in the correct places,
a sensible number of digits, and no syntax highlighting).  It's a bit cumbersome to use

    `ri I(prettyNum(round(x,2),big.mark=",")) ir`

each time, instead of just

    `ri x ir`

(using Yuhui's shortened github patterns in for inline code in these examples). 
Just wondering.

Thanks!

- Carl
--
Carl Boettiger
UC Davis
http://www.carlboettiger.info/

Yihui Xie

unread,
Apr 9, 2012, 4:02:55 PM4/9/12
to Carl Boettiger, knitr
As usual, you can reset the default hooks; in this case, it is the inline hook:

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

Actually rounding comes by default according to getOption('digits'),
but prettyNum() is not supported in the default hook. Almost
everything about output in knitr is a matter of default vs
customization. I have to make the default more general, so I cannot
make the comma format into the default hook. Of course I can make the
default hook accept outside options, as long as they are convincing.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Jeff van Geete

unread,
May 7, 2015, 3:09:52 PM5/7/15
to kn...@googlegroups.com, cboe...@gmail.com
Is there a way to have the plot axis markings follow the same format as what the hook has set? These options settings are working for my text-based output, but the (margin) figures still have '8000000' (eg)

Rlastra

unread,
Feb 16, 2018, 1:01:45 PM2/16/18
to knitr
This work for me Jeff....i hope it helps!!

hint: use ggplot and is thinking when your values are on "y" axis

  geom_label(aes(label=(paste0(Nameofvariable,"%"," Sin"))),size=3,hjust=0, vjust=0)+
  stat_summary(fun.y = sum,
               aes(label = format(..y.., big.mark = ",")),
               geom = "label",
               vjust = 0,
               hjust = -1,
               size =3)
Reply all
Reply to author
Forward
0 new messages