Highlighting bash code with knitr / rmarkdown

前端 未结 2 1766
春和景丽
春和景丽 2020-12-31 14:20

I\'m trying to generate an HTML report using RStudio, R Markdown and knitr. In the report I would like to display some bash code. I do not want to run the code

相关标签:
2条回答
  • 2020-12-31 14:41

    The default syntax highlighting theme does not work well for non-R code chunks, and you can use other themes, e.g. pygments

    ---
    title: "Bash Highlighting"
    output:
      html_document:
        highlight: pygments
    ---
    
    ```{r, engine = 'bash', eval = FALSE}
    for foo in (ls bar)
    do
      echo $foo
    done
    ```
    
    0 讨论(0)
  • 2020-12-31 15:01

    OK, figured it out thanks to the comments. It seems it's RStudio which is not playing nice with the highlighting. When I keep the intermediate markdown file as in.md:

    ---
    title: "Bash Highlighting"
    output:
      html_document:
        keep_md: true
    ---
    
    ```{r, engine = 'bash', eval = FALSE}
    for foo in (ls bar)
    do
      echo $foo
    done
    ```
    

    then convert to html with pandoc using e.g. the CSS from BioConductor:

    pandoc -s in.md \
        -c https://hedgehog.fhcrc.org/bioconductor/branches/RELEASE_3_1/madman/Rpacks/BiocStyle/inst/resources/html/bioconductor.css \
        -t html -o out.html
    

    I get nice code highlighting for R and bash.

    Thanks!

    0 讨论(0)
提交回复
热议问题