Knitr syntax highlighting?

130 views
Skip to first unread message

Carl Boettiger

unread,
Aug 4, 2014, 1:20:23 PM8/4/14
to knitr
Hi Yihui, list,

If I recall correctly, knitr's default markdown syntax, ```{r} was
intended to be a valid markdown syntax. It seems Github Flavored
markdown is happy to recognize this as markdown syntax for a code
block in the R language, but while pandoc recognizes this as a code
block, it does not recognize the language.

I believe Pandoc does recognize the format ```{.r options} as the
notation to specify the R language in this notation. I like this
notation because it means that pandoc-aware syntax highlighters will
highlight my code chunks as R code, which does not happen in the
default syntax. (I'm using vim-pandoc, which I highly recommend)

I realize I could define this as an input hook for knitr, but am
reluctant to do so as it makes my code slightly less familiar/less
portable to other users (e.g. RStudio expects and integrates with only
the standard notation.)

I was wondering if others have encountered this issue or ways to work
around it, and if default/RStudio support for ```{.r syntax would be a
reasonable feature request or just a can of worms.

Cheers,

Carl

--
Carl Boettiger
UC Santa Cruz
http://carlboettiger.info/

Joshua Wiley

unread,
Aug 4, 2014, 7:16:12 PM8/4/14
to Carl Boettiger, knitr
Hi Carl,

An option (a bit more work, but still) would be to just use ```{.r and then have a script that processes files to or from that:

fromVIM <- function(file) {
  x <- readLines(file)
  x <- gsub("^```\\{\\.r", "```{r", x)
  writeLines(x, con = file)
}


toVIM <- function(file) {
  x <- readLines(file)
  x <- gsub("^```\\{r", "```{.r", x)
  writeLines(x, con = file)
}

Those pick up ```{.r  or ```{r at the beginnings of lines only, so you could safely have those in the middle of a line (e.g., if writing a note on how you make it work in vim.

Just a note that polymode in Emacs recognizes the R chunks fine as is, so it is not just Rstudio that works with ```{r} --- I wonder if it would be possible to alter vim-pandoc?

Cheers,

Josh





--
You received this message because you are subscribed to the Google Groups "knitr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knitr+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Joshua F. Wiley
Ph.D. Student, UCLA Department of Psychology
http://joshuawiley.com/
Senior Analyst, Elkhart Group Ltd.
http://elkhartgroup.com
Office: 260.673.5518

Jeff Newmiller

unread,
Aug 4, 2014, 7:26:31 PM8/4/14
to Joshua Wiley, kn...@googlegroups.com, Carl Boettiger

Re vim: Quite likely, but markdown per se is an output of knitr, so the logical thing to do is recognize the distinction between knitr-pandoc and pandoc by making a new vim-knitr-pandoc highlight definitions file.

Disclaimer: I don't use vim for any significant amount of work, so it might be a chore to do any of this.

Yihui Xie

unread,
Aug 14, 2014, 5:41:17 PM8/14/14
to Carl Boettiger, knitr
The current definition of chunk headers in knitr is

# knitr::all_patterns$md$chunk.begin

"^\\s*```+\\s*\\{r(.*)\\}\\s*$"

I can certainly change it to

"^\\s*```+\\s*\\{[.]?r(.*)\\}\\s*$"

This is fairly easy for me to do (perhaps also easy for polymode to
change), but I guess it will more difficult for RStudio to change. You
can try to submit a pull request or feature request to vim-pandoc
first. If its author says no, I can make the syntax ```{.r } valid in
knitr, although it may confuse your collaborators/readers when they
open the document in RStudio.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name

Carl Boettiger

unread,
Aug 14, 2014, 5:57:45 PM8/14/14
to Yihui Xie, knitr
Thanks.  Yeah, the vim-pandoc authors are reluctant to implement something that's not actually part of the pandoc syntax.

Knitr handling both definitions as you suggest would resolve everything but the RStudio editor's behaviour; do you forsee this being something RStudio would consider?  It wouldn't break anything but it would introduce an alternative convention which could always cause confusion.  It would certainly be nice that knitr's (input) markdown could be completely valid pandoc markdown, but hard to say if it's worth having two conventions. 

Yihui Xie

unread,
Aug 14, 2014, 6:00:53 PM8/14/14
to Carl Boettiger, knitr
I'll ask RStudio IDE developers and see what they think.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Yihui Xie

unread,
Aug 14, 2014, 6:53:11 PM8/14/14
to Carl Boettiger, knitr
Okay, their answer is yes, but it may take a couple of months for this
to happen. I'll make this change in knitr first.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Carl Boettiger

unread,
Aug 14, 2014, 7:09:03 PM8/14/14
to Yihui Xie, knitr
very cool and many thanks
Reply all
Reply to author
Forward
0 new messages