将SCons与Knitr结合使用的示例 [英] Examples of using SCons with knitr

查看:99
本文介绍了将SCons与Knitr结合使用的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用 SCons knitr生成报告的最小甚至更大的工作示例来自.Rmd个文件?

Are there minimal, or even larger, working examples of using SCons and knitr to generate reports from .Rmd files?

knit从命令行(bash shell)获取cleaning_session.Rmd文件以导出.html文件,可以通过以下方式完成:

kniting an cleaning_session.Rmd file from the command line (bash shell) to derive an .html file, may be done via:

Rscript -e "library(knitr); knit('cleaning_session.Rmd')".

在此示例中,Rscript和说明被馈送到Make文件:

In this example, Rscript and instructions are fed to a Makefile:

RMDFILE=test

html :
    Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''

在此答案中 https://stackoverflow.com/a/10945832/1172302 ,据报道存在一种使用以下解决方案的解决方案SCons.但是,我没有进行足够的测试来使其适合我.从本质上讲,像 https://tex.stackexchange.com/a/26573/上显示的示例一样棒8272 .

In this answer https://stackoverflow.com/a/10945832/1172302, there is reportedly a solution using SCons. Yet, I did not test enough to make it work for me. Essentially, it would be awesome to have something like the example presented at https://tex.stackexchange.com/a/26573/8272.

推荐答案

[已更新]一个有效的示例是Sconstruct文件:

[Updated] One working example is an Sconstruct file:

import os
environment = Environment(ENV=os.environ)

# define a `knitr` builder
builder = Builder(action = '/usr/local/bin/knit $SOURCE -o $TARGET',
                  src_suffix='Rmd')

# add builders as "Knit", "RMD"
environment.Append( BUILDERS = {'Knit' : builder} )

# define an `rmarkdown::render()` builder
builder = Builder(action = '/usr/bin/Rscript -e "rmarkdown::render(input=\'$SOURCE\', output_file=\'$TARGET\')"',
        src_suffix='Rmd')

environment.Append( BUILDERS = {'RMD' : builder} )

# define source (and target files -- currently useless, since not defined above!)

# main cleaning session code
environment.RMD(source='cleaning_session.Rmd', target='cleaning_session.html')

# documentation of the Cleaning Process
environment.Knit(source='Cleaning_Process.Rmd', target='Cleaning_Process.html')

# documentation of data
environment.Knit(source='Code_Book.Rmd', target='Code_Book.html')

  • 第一个 builder 调用名为knit的自定义脚本.依次处理目标文件/扩展名,此处为cleaning_session.html.在这个示例中,可能完全不需要suffix参数.

    • The first builder calls the custom script called knit. Which, in turn, takes care of the target file/extension, here being cleaning_session.html. Likely the suffix parameter is not needed altogether, in this very example.

      添加的第二个 builder Rscript -e "rmarkdown::render(\'$SOURCE\')"'.

      $TARGET的存在(例如命令包装器)可确保如果目标文件已存在,SCons不会重复工作.

      The existence of $TARGETs (as in the example at Command wrapper) ensures SCons won't repeat work if a target file already exists.

      自定义脚本(当前无法获取其来源)是:

      The custom script (whose source I can't retrieve currently) is:

      #!/usr/bin/env Rscript
      
      local({
        p = commandArgs(TRUE)
        if (length(p) == 0L || any(c('-h', '--help') %in% p)) {
          message('usage: knit input [input2 input3] [-n] [-o output output2 output3]
          -h, --help        to print help messages
          -n, --no-convert  do not convert tex to pdf, markdown to html, etc
          -o                output filename(s) for knit()')
          q('no')
        }
      
        library(knitr)
        o = match('-o', p)
        if (is.na(o)) output = NA else {
          output = tail(p, length(p) - o)
          p = head(p, o - 1L)
        }
        nc = c('-n', '--no-convert')
        knit_fun = if (any(nc %in% p)) {
          p = setdiff(p, nc)
          knit
        } else {
          if (length(p) == 0L) stop('no input file provided')
          if (grepl('\\.(R|S)(nw|tex)$', p[1])) {
            function(x, ...) knit2pdf(x, ..., clean = TRUE)
          } else {
            if (grepl('\\.R(md|markdown)$', p[1])) knit2html else knit
          }
        }
      
        mapply(knit_fun, p, output = output, MoreArgs = list(envir = globalenv()))
      })
      

      现在唯一需要做的就是运行scons.

      The only thing, now, necessary is to run scons.

      这篇关于将SCons与Knitr结合使用的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆