knit_patterns: Patterns to match and extract R code in a document

Description Usage Format References See Also Examples

Description

Patterns are regular expressions and will be used in functions like base::grep() to extract R code and chunk options. The object knit_patterns controls the patterns currently used; see the references and examples for usage. All built-in patterns are available in the list all_patterns.

Usage

1

Format

An object of class list of length 6.

References

Usage: https://yihui.org/knitr/objects/

Components in knit_patterns: https://yihui.org/knitr/patterns/

See Also

all_patterns

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
library(knitr)
opat = knit_patterns$get()  # old pattern list (to restore later)

apats = all_patterns  # a list of all built-in patterns
str(apats)
knit_patterns$set(apats[["rnw"]])  # set pattern list from apats

knit_patterns$get(c("chunk.begin", "chunk.end", "inline.code"))

# a customized pattern list; has to empty the original patterns first!
knit_patterns$restore()
# we may want to use this in an HTML document
knit_patterns$set(list(chunk.begin = "<!--helloR\\s+(.*)", chunk.end = "^byeR-->"))
str(knit_patterns$get())

knit_patterns$set(opat)  # put the old patterns back

Example output

List of 8
 $ rnw     :List of 7
  ..$ chunk.begin   : chr "^\\s*<<(.*)>>=.*$"
  ..$ chunk.end     : chr "^\\s*@\\s*(%+.*|)$"
  ..$ inline.code   : chr "\\\\Sexpr\\{([^}]+)\\}"
  ..$ inline.comment: chr "^\\s*%.*"
  ..$ ref.chunk     : chr "^\\s*<<(.+)>>\\s*$"
  ..$ header.begin  : chr "(^|\n)\\s*\\\\documentclass[^}]+\\}"
  ..$ document.begin: chr "\\s*\\\\begin\\{document\\}"
 $ brew    :List of 1
  ..$ inline.code: chr "<%[=]{0,1}\\s+([^%]+)\\s+[-]*%>"
 $ tex     :List of 8
  ..$ chunk.begin   : chr "^\\s*%+\\s*begin.rcode\\s*(.*)"
  ..$ chunk.end     : chr "^\\s*%+\\s*end.rcode"
  ..$ chunk.code    : chr "^\\s*%+"
  ..$ ref.chunk     : chr "^%+\\s*<<(.+)>>\\s*$"
  ..$ inline.comment: chr "^\\s*%.*"
  ..$ inline.code   : chr "\\\\rinline\\{([^}]+)\\}"
  ..$ header.begin  : chr "(^|\n)\\s*\\\\documentclass[^}]+\\}"
  ..$ document.begin: chr "\\s*\\\\begin\\{document\\}"
 $ html    :List of 5
  ..$ chunk.begin : chr "^\\s*<!--\\s*begin.rcode\\s*(.*)"
  ..$ chunk.end   : chr "^\\s*end.rcode\\s*-->"
  ..$ ref.chunk   : chr "^\\s*<<(.+)>>\\s*$"
  ..$ inline.code : chr "<!--\\s*rinline(.+?)-->"
  ..$ header.begin: chr "\\s*<head>"
 $ md      :List of 4
  ..$ chunk.begin: chr "^[\t >]*```+\\s*\\{([a-zA-Z0-9]+.*)\\}\\s*$"
  ..$ chunk.end  : chr "^[\t >]*```+\\s*$"
  ..$ ref.chunk  : chr "^\\s*<<(.+)>>\\s*$"
  ..$ inline.code: chr "`r[ #]([^`]+)\\s*`"
 $ rst     :List of 5
  ..$ chunk.begin: chr "^\\s*[.][.]\\s+\\{r(.*)\\}\\s*$"
  ..$ chunk.end  : chr "^\\s*[.][.]\\s+[.][.]\\s*$"
  ..$ chunk.code : chr "^\\s*[.][.]"
  ..$ ref.chunk  : chr "^\\.*\\s*<<(.+)>>\\s*$"
  ..$ inline.code: chr ":r:`([^`]+)`"
 $ asciidoc:List of 6
  ..$ chunk.begin   : chr "^//\\s*begin[.]rcode(.*)$"
  ..$ chunk.end     : chr "^//\\s*end[.]rcode\\s*$"
  ..$ chunk.code    : chr "^//+"
  ..$ ref.chunk     : chr "^\\s*<<(.+)>>\\s*$"
  ..$ inline.code   : chr "`r +([^`]+)\\s*`|[+]r +([^+]+)\\s*[+]"
  ..$ inline.comment: chr "^//.*"
 $ textile :List of 5
  ..$ chunk.begin   : chr "^###[.]\\s+begin[.]rcode(.*)$"
  ..$ chunk.end     : chr "^###[.]\\s+end[.]rcode\\s*$"
  ..$ ref.chunk     : chr "^\\s*<<(.+)>>\\s*$"
  ..$ inline.code   : chr "@r +([^@]+)\\s*@"
  ..$ inline.comment: chr "^###[.].*"
$chunk.begin
[1] "^\\s*<<(.*)>>=.*$"

$chunk.end
[1] "^\\s*@\\s*(%+.*|)$"

$inline.code
[1] "\\\\Sexpr\\{([^}]+)\\}"

List of 9
 $ chunk.begin   : chr "<!--helloR\\s+(.*)"
 $ chunk.end     : chr "^byeR-->"
 $ chunk.code    : NULL
 $ inline.code   : NULL
 $ global.options: NULL
 $ input.doc     : NULL
 $ ref.chunk     : NULL
 $ header.begin  : NULL
 $ document.begin: NULL

knitr documentation built on Jan. 27, 2021, 5:09 p.m.