gpt4 book ai didi

r - 在 knitr/markdown 中合并来自外部文件的代码列表

转载 作者:行者123 更新时间:2023-12-03 22:45:28 26 4
gpt4 key购买 nike

我想将从外部文件中提取的代码列表合并到 Rmarkdown 文件中。我想要它漂亮(语法高亮、自动缩进等)。

  • 该代码不是 R 代码(否则我可以使用一些现有的技巧来 pretty-print R functions ) - 具体来说,它是 BUGSStan代码。
  • 我不一定针对 LaTeX/PDF 输出:否则我可以使用 listings包裹。
  • 我希望能够在没有笨重的外部 cat firstpart.rmd codefile.rmd lastpart.rmd >wholefile.rmd 的情况下合并文件系统命令,并且没有预处理步骤:this question建议像 Multimarkdown 和 Marked 2 这样的 Markdown 处理器具有文件包含语法,但我认为我坚持使用 pandoc。
  • 目前我正在使用这样的代码块

  • ```{r jagsmodel, echo=FALSE, results="markup", comment=""}
    cat(readLines("logist.bug"),sep="\n")
    ```

    这工作正常,但没有让我语法突出显示......

    最佳答案

    这是一种方法。您需要pygments已安装( pip install pygments )并且它必须能够放置(它应该自己)“pygmentize 在您系统的某个地方。如果您没有python或无法安装该模块,那么这个答案显然是不会是你的答案。

    以下 knitr block (除了最后一个是纯 R 之外的所有 block )调用 pygmentize为代码创建 HTML 标记。应该可以修改https://github.com/hrbrmstr/knitrengines使这更加“自动”,但这不在我的短期 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)
    ```

    这使得:

    enter image description here

    您可以将任何您喜欢的 pygments CSS 文件用于不同的方案,这就是 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>

    #

    更新

    如果您愿意将代码(不会被执行但会被格式化)内联,我刚刚更新了前面提到的 knitrengines允许您执行此操作的软件包:
    ---
    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)
    for i := 0; i < 3; i++ {
    innerLen := i + 1
    twoD[i] = make([]int, innerLen)
    for j := 0; j < innerLen; j++ {
    twoD[i][j] = i + j
    }
    }
    fmt.Println("2d: ", twoD)
    }
    ```

    ```{pygments test3, pyg.ext="ex", pyg.sty="github"}
    defmodule Math do
    def sum(a, b) do
    a + b
    end
    end

    IO.puts "The answer is #{ Math.sum(4,3) }"
    ```

    并产生这个:

    enter image description here

    我可以让它对每个代码块使用不同的样式,但我几乎没有周期来完成它。这不应该是太多的工作。

    关于r - 在 knitr/markdown 中合并来自外部文件的代码列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857212/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com