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.
knit_patterns
An object of class list
of length 7.
Usage: https://yihui.org/knitr/objects/
Components in knit_patterns
: https://yihui.org/knitr/patterns/
library(knitr)
opat = knit_patterns$get() # old pattern list (to restore later)
apats = all_patterns # a list of all built-in patterns
str(apats)
#> 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 "(?<!(^``))(?<!(\n``))`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 "^###[.].*"
knit_patterns$set(apats[["rnw"]]) # set pattern list from apats
knit_patterns$get(c("chunk.begin", "chunk.end", "inline.code"))
#> $chunk.begin
#> [1] "^\\s*<<(.*)>>=.*$"
#>
#> $chunk.end
#> [1] "^\\s*@\\s*(%+.*|)$"
#>
#> $inline.code
#> [1] "\\\\Sexpr\\{([^}]+)\\}"
#>
# 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())
#> 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
knit_patterns$set(opat) # put the old patterns back