Examples of using SCons with knitr

别等时光非礼了梦想. 提交于 2019-12-12 02:33:47

问题


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

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')".

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=''

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.


回答1:


[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')
  • 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.

  • The second builder added is Rscript -e "rmarkdown::render(\'$SOURCE\')"'.

  • 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()))
})

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



来源:https://stackoverflow.com/questions/38769811/examples-of-using-scons-with-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!