r - incorporate code listings from an external file in knitr/markdown -


i incorporate listings of code drawn external files in rmarkdown file. pretty (syntax highlighting, auto-indentation, etc.).

  • the code not r code (otherwise use of existing tricks pretty-print r functions) - specifically, it's bugs , stan code.
  • i'm not targeting latex/pdf output: otherwise use listings package.
  • i'd able incorporate files without unwieldy external cat firstpart.rmd codefile.rmd lastpart.rmd >wholefile.rmd system command, , without pre-processing step: this question suggests markdown processors multimarkdown , marked 2 have file inclusion syntax, think i'm stuck pandoc.
  • at present i'm using code chunks this
```{r jagsmodel, echo=false, results="markup", comment=""} cat(readlines("logist.bug"),sep="\n") ``` 

which works ok doesn't me syntax highlighting ...

here's 1 approach. need pygments installed (pip install pygments) , has able put (it should on it's own) "pygmentize somewhere on system. if don't have python or can't install module, answer not going answer you.

the following knitr chunks (all last 1 pure r) call pygmentize create html markup code. should possible modify https://github.com/hrbrmstr/knitrengines make more "automagical" that's not on short-term todo.

--- title: "lexers" output:    html_document:     css: code.css     keep_md: true ---  ```{r setup, include=false} knitr::opts_chunk$set(echo = true) ```  ### bugs  ```{r bugs1, echo=false, results="asis"} tf <- tempfile(fileext=".html") system(sprintf("/usr/local/bin/pygmentize -o %s incl.bug", tf)) cat(readlines(tf), sep="\n") unlink(tf) ```  ### elixir  ```{r elixir1, echo=false, results="asis"} tf <- tempfile(fileext=".html") system(sprintf("/usr/local/bin/pygmentize -o %s incl.ex", tf)) cat(readlines(tf), sep="\n") unlink(tf) ```  ### python  ```{r py1, echo=false, results="asis"} tf <- tempfile(fileext=".html") system(sprintf("/usr/local/bin/pygmentize -o %s incl.py", tf)) cat(readlines(tf), sep="\n") unlink(tf) ```  ### plain ol' r  ```{r} summary(mtcars) ``` 

that makes:

enter image description here

you can use pygments css file different scheme, what's in code.css:

<style> .gl .hll { background-color: #ffffcc } .highlight  { background: #f8f8f8; } .highlight .c { color: #408080; font-style: italic } /* comment */ .highlight .err { border: 1px solid #ff0000 } /* error */ .highlight .k { color: #008000; font-weight: bold } /* keyword */ .highlight .o { color: #666666 } /* operator */ .highlight .cm { color: #408080; font-style: italic } /* comment.multiline */ .highlight .cp { color: #bc7a00 } /* comment.preproc */ .highlight .c1 { color: #408080; font-style: italic } /* comment.single */ .highlight .cs { color: #408080; font-style: italic } /* comment.special */ .highlight .gd { color: #a00000 } /* generic.deleted */ .highlight .ge { font-style: italic } /* generic.emph */ .highlight .gr { color: #ff0000 } /* generic.error */ .highlight .gh { color: #000080; font-weight: bold } /* generic.heading */ .highlight .gi { color: #00a000 } /* generic.inserted */ .highlight .go { color: #808080 } /* generic.output */ .highlight .gp { color: #000080; font-weight: bold } /* generic.prompt */ .highlight .gs { font-weight: bold } /* generic.strong */ .highlight .gu { color: #800080; font-weight: bold } /* generic.subheading */ .highlight .gt { color: #0040d0 } /* generic.traceback */ .highlight .kc { color: #008000; font-weight: bold } /* keyword.constant */ .highlight .kd { color: #008000; font-weight: bold } /* keyword.declaration */ .highlight .kn { color: #008000; font-weight: bold } /* keyword.namespace */ .highlight .kp { color: #008000 } /* keyword.pseudo */ .highlight .kr { color: #008000; font-weight: bold } /* keyword.reserved */ .highlight .kt { color: #b00040 } /* keyword.type */ .highlight .m { color: #666666 } /* literal.number */ .highlight .s { color: #ba2121 } /* literal.string */ .highlight .na { color: #7d9029 } /* name.attribute */ .highlight .nb { color: #008000 } /* name.builtin */ .highlight .nc { color: #0000ff; font-weight: bold } /* name.class */ .highlight .no { color: #880000 } /* name.constant */ .highlight .nd { color: #aa22ff } /* name.decorator */ .highlight .ni { color: #999999; font-weight: bold } /* name.entity */ .highlight .ne { color: #d2413a; font-weight: bold } /* name.exception */ .highlight .nf { color: #0000ff } /* name.function */ .highlight .nl { color: #a0a000 } /* name.label */ .highlight .nn { color: #0000ff; font-weight: bold } /* name.namespace */ .highlight .nt { color: #008000; font-weight: bold } /* name.tag */ .highlight .nv { color: #19177c } /* name.variable */ .highlight .ow { color: #aa22ff; font-weight: bold } /* operator.word */ .highlight .w { color: #bbbbbb } /* text.whitespace */ .highlight .mf { color: #666666 } /* literal.number.float */ .highlight .mh { color: #666666 } /* literal.number.hex */ .highlight .mi { color: #666666 } /* literal.number.integer */ .highlight .mo { color: #666666 } /* literal.number.oct */ .highlight .sb { color: #ba2121 } /* literal.string.backtick */ .highlight .sc { color: #ba2121 } /* literal.string.char */ .highlight .sd { color: #ba2121; font-style: italic } /* literal.string.doc */ .highlight .s2 { color: #ba2121 } /* literal.string.double */ .highlight .se { color: #bb6622; font-weight: bold } /* literal.string.escape */ .highlight .sh { color: #ba2121 } /* literal.string.heredoc */ .highlight .si { color: #bb6688; font-weight: bold } /* literal.string.interpol */ .highlight .sx { color: #008000 } /* literal.string.other */ .highlight .sr { color: #bb6688 } /* literal.string.regex */ .highlight .s1 { color: #ba2121 } /* literal.string.single */ .highlight .ss { color: #19177c } /* literal.string.symbol */ .highlight .bp { color: #008000 } /* name.builtin.pseudo */ .highlight .vc { color: #19177c } /* name.variable.class */ .highlight .vg { color: #19177c } /* name.variable.global */ .highlight .vi { color: #19177c } /* name.variable.instance */ .highlight .il { color: #666666 } /* literal.number.integer.long */ </style> 
#

update

if you're willing put code (that won't executed formatted) inline, made update aforementioned knitrengines package lets this:

--- title: "pygtest" output: html_document ---  ```{r setup, include=false} knitr::opts_chunk$set(echo = true) library(knitrengines) ```  ```{pygments test, pyg.ext="py", pyg.sty="github"} import something.please  print("hello, world!") ```  ```{pygments test2, pyg.ext="go", pyg.sty="github"} package main  import "fmt"  func main() {      s := make([]string, 3)     fmt.println("emp:", s)      s[0] = "a"     s[1] = "b"     s[2] = "c"     twod := make([][]int, 3)     := 0; < 3; i++ {         innerlen := + 1         twod[i] = make([]int, innerlen)         j := 0; j < innerlen; j++ {             twod[i][j] = + j         }     }     fmt.println("2d: ", twod) } ```  ```{pygments test3, pyg.ext="ex", pyg.sty="github"} defmodule math   def sum(a, b)     + b   end end  io.puts "the answer #{ math.sum(4,3) }"     ``` 

and produces this:

enter image description here

i can make use different styles per code block, barely had cycles crank out. shouldn't work tho.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -