【问题标题】:Can Rmarkdown print a file's C++ code on GitHub?Rmarkdown 可以在 GitHub 上打印文件的 C++ 代码吗?
【发布时间】:2021-06-22 22:52:09
【问题描述】:

Rmarkdown 可以读取文件并使用打印它

```{r}
fileName <- '../code/file.cpp'
readChar(fileName, file.info(fileName)$size)
`` `

(末尾没有空格)

是否可以在其中使用语法着色? 这个想法是,这将被转换为

```cpp
contents of the file
`` `

(末尾没有空格)

在转换为 HTML 文档之前。

非常感谢任何愿意回答的人,节日快乐!

【问题讨论】:

    标签: c++ github r-markdown markdown


    【解决方案1】:

    此时我能做的最好的就是复制整个 C++ 代码,将其粘贴到 pdf_document 输出 Rmarkdown 文件中的 Rcpp{} 块中,这将使用您的彩色 C++ 代码呈现。

    ---
    title: "test"
    author: "Daniel"
    date: "3/31/2021"
    output: pdf_document
    ---
    
    
    
    ```{Rcpp}
    #include <Rcpp.h>
    using namespace Rcpp;
    
    // This is a simple example of exporting a C++ function to R. You can
    // source this function into an R session using the Rcpp::sourceCpp 
    // function (or via the Source button on the editor toolbar). Learn
    // more about Rcpp at:
    //
    //   http://www.rcpp.org/
    //   http://adv-r.had.co.nz/Rcpp.html
    //   http://gallery.rcpp.org/
    //
    
    // [[Rcpp::export]]
    NumericVector timesTwo(NumericVector x) {
      return x * 2;
    }
    
    
    // You can include R code blocks in C++ files processed with sourceCpp
    // (useful for testing and development). The R code will be automatically 
    // run after the compilation.
    //
    
    /*** R
    timesTwo(42)
    */
    ```
    

    【讨论】:

    • 并非如此。将你的 c++ 代码放在一个文件中,并找到一种方法让它出现在带有语法着色的 pdf 中。这就是我要找的:)
    • 我更新了我的答案,最好是单张打印,而不是运行报告,因为它有一个手动过程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    相关资源
    最近更新 更多