【问题标题】:Jekyll Converter for R Markdown用于 R Markdown 的 Jekyll 转换器
【发布时间】:2012-11-27 10:44:44
【问题描述】:

我正在尝试为 R Markdown 文件编写 Jekyll 转换器。我创建了RMarkdownConverter.rb 并将其放在我的_plugins 目录中。我已经验证了其他插件正在工作,但这个不是。我也没有看到任何错误消息,包括我自己输入的错误消息。这似乎没有被使用。然而,Jekyll 正在为我的.Rmd 文件生成一个 HTML 文件,但只是将 R 卡盘处理为代码卡盘。任何帮助或想法将不胜感激。

RMarkdownConverter.rb文件:

module Jekyll
    class RMarkdownConverter < Converter
        safe true
        priority :low

    def setup
      STDERR.puts "Setting up R Markdown..."
      return if @setup
      require 'rinruby'
      @setup = true
    rescue
      STDERR.puts 'do `gem install rinruby`'
      raise FatalException.new("Missing dependency: rinruby")
    end

        def matches(ext)
            ext =~ /Rmd/i
        end

        def output_ext(ext)
           '.html'
        end

        def convert(content)
      setup
      STDERR.puts "Using R Markdown..."
      R.eval "require(knitr)"
      R.eval "render_markdown(strict=TRUE)"
      R.assign "content", content
      STDERR.puts content
      R.eval "out <- knit(text=content)"
      R.eval "print(out)"
        end
    end
end

我的第一篇 R Markdown 帖子的内容:

--- 
layout: post
title: Using (R) Markdown, Jekyll, and Github for Blogging
published: true
tags: R R-Bloggers Jekyll github
type: post
status: publish
---

First, we need to install [RinRuby](https://sites.google.com/a/ddahl.org/rinruby-users/) to call R from Ruby. In the terminal, execute:

    gem install rinruby

First R chuck:

```{r}
2 + 2
```

【问题讨论】:

    标签: r jekyll knitr r-markdown


    【解决方案1】:

    尝试将最后几行替换为以下内容

    R.assign "content", content
    R.eval "knitr::render_markdown(strict = TRUE)"
    R.pull "(knitr::knit2html(text = content, fragment.only = TRUE))"
    

    我认为您需要 R.pull 将 R 输出的内容复制到 Ruby。此外,我建议直接从 Rmd 转换为 html。我已成功使用此策略与 Ruhoh 合作,这是另一个基于 ruby​​ 的博客平台。

    更新。这很奇怪,但使用扩展名 rmd 似乎与 md 冲突。我将它随机更改为 ram 并且 jekyll 似乎正确地选择了它。我不知道为什么。

    【讨论】:

    • 如果你将markdown_ext: markdown 添加到_config.yml,Jekyll 将处理rmd 文件。但是,这也意味着不会处理md 文件。对我来说没什么大不了的,因为我一直在使用 .markdown 文件扩展名。
    • 应该有一种方法可以做到这一点,而无需调用已知很慢的rinruby。我正在探索如何直接使用 shell 命令来处理 Rmd 文件,但是 knitr 使用的反引号会导致 shell 执行失败。
    猜你喜欢
    • 2013-11-15
    • 1970-01-01
    • 2014-04-02
    • 2017-05-11
    • 2016-06-07
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多