使用knitr时,在将块拆分成单独的文件时保留块选项 [英] With knitr, preserve chunk options when purling chunks into separate files

查看:95
本文介绍了使用knitr时,在将块拆分成单独的文件时保留块选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于教学目的,我想将.Rnw文件的大块purl分成单独的文件.此答案说明了如何执行此操作:

For teaching purposes, I would like to purl the chunks of my .Rnw file into separate files. This answer explains how to do it:

如何使用Knitr将.Rmd文件中的每个块分解为多个.R文件

但是该方法不保留块选项.由于我拥有的块会产生图,因此保留fig.widthfig.height选项很重要.理想情况下,我想要一个看起来像这样的块:

BUT the method does not preserve the chunk options. Since the chunks I have produce plots, it's important to preserve the fig.width and fig.height options. Ideally I would like a chunk that looks like this:

<<plot, fig.width = 3, fig.height = 5, outwidth = '.75\\textwidth'>>=
plot (1,1)
@

变成名为plot.R的文件,如下所示:

to become a file named plot.R that looks like this:

#+ fig.width = 3, fig.height = 5
plot (1,1)

也就是说,将块选项fig.widthfig.height转换为spin()可以识别的格式,就像purl()一样,并摆脱不相关的块选项,否则会产生问题将spin()转换为Word,例如out.width.一切都是为了创建易于使用的代码文件.

That is, turn the chunk options fig.width and fig.height into a format that will be recognized by spin(), as purl() does, and get rid of the chunk options that are irrelevant, or create problems for spin() into Word, such as out.width. All in the spirit of creating code files that are user-friendly.

推荐答案

由于您引用的答案不会复制purl结果中的标题行,因此您会丢失除块名之外的所有内容.尽管您可以对其进行调整以粘贴到标头中,但实际上并不难构建一个函数来解析purl的输出-比尝试解析RmdRnw文档要容易得多,无论如何,比排序更容易明确说明knitr的操作方式.

Since the answer you refer to doesn't copy the header line from the results of purl, you lose everything besides the chunk name. While you could adapt it to paste in the headers, it's actually not hard to build a function to parse the output of purl—much easier than trying to parse a Rmd or Rnw document, anyway, and easier than sorting out exactly how knitr does so.

purl_chunks <- function(input_file){
  purled <- knitr::purl(input_file)    # purl original file; save name to variable
  lines <- readLines(purled)    # read purled file into a character vector of lines
  starts <- grep('^## ----.*-+', lines)    # use grep to find header row indices
  stops <- c(starts[-1] - 1L, length(lines))   # end row indices
  # extract chunk names from headers
  names <- sub('^## ----([^-]([^,=]*[^,=-])*)[,-][^=].*', '\\1', lines[starts])
  names <- ifelse(names == lines[starts], '', paste0('_', names)) # clean if no chunk name
  # make nice file names with chunk number and name (if exists)
  file_names <- paste0('chunk_', seq_along(starts), names, '.R')
  for(chunk in seq_along(starts)){    # loop over header rows
    # save the lines in the chunk to a file
    writeLines(lines[starts[chunk]:stops[chunk]], con = file_names[chunk])
  }
  unlink(purled)    # delete purled file of entire document
}

一些注意事项:

  • 尽管正则表达式可以满足我的要求,但它仍然可能会出错.经测试:
    • 没有块名
    • 没有名字,只有块设置
    • 带连字符的名称
    • 单个字符名
    • 带空格的名称,包括(在逗号/大括号之前)
    • While the regex works for what I've thrown at it, it may yet be fallible. Tested:
      • no chunk name
      • no name but chunk settings
      • name with hyphens
      • single character names
      • names with spaces, including after (before the comma/brace)

      这篇关于使用knitr时,在将块拆分成单独的文件时保留块选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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