Pandoc RMarkdown Testing

This doesn’t work with the default render settings for html, but using highlight: pygments works nicely.

Definition of a numbering chunk hook.

````{r default_chunk_hook, eval=FALSE, echo=FALSE}

# The current chunk hook may not be the default, and should be processed
# prior to doing the numbering; which should come last.

previous_chunk_hook <- knitr::knit_hooks$get("chunk")

knitr::knit_hooks$set(chunk = function(x, options) {
    
    x <- previous_chunk_hook(x, options)
    
    if (isTRUE(options$number)) {
        
        str <- "{.r .number"
        if (!is.null(options$startFrom)) {
            str <- paste0(str, " startFrom=\"", options$startFrom, "\"")
        }
        str <- paste0(str, "}")
        
        x <- gsub("(\\s?[`]{3,})r", paste0("\\1", str), x)
        
    }
    
    return(gsub("(^\n|\n+$)", "", x))
    
})
````

Markdown to achieve highlighted chunk numbering.

````{r default_chunk_hook, eval=FALSE, number=TRUE}
````

Output prior to activating the hook.

# The current chunk hook may not be the default, and should be
# processed prior to doing the numbering; which should come last.

previous_chunk_hook <- knitr::knit_hooks$get("chunk")

knitr::knit_hooks$set("chunk"=function(x,options){
  
  x <- previous_chunk_hook(x, options)
  
  if( isTRUE( options$number ) ) {
    
    str <- "{.r .number"
    if( !is.null(options$startFrom) ) {
      str <- paste0( str, " startFrom=\"", options$startFrom,"\"")
    }
    str <- paste0(str, "}")
    
    x <- gsub( "(\\s?[`]{3,})r", paste0( "\\1", str ), x)

  }

  return( gsub( "(^\n|\n+$)", "", x ) )

})

Activate the hook.

````{r default_chunk_hook, echo=FALSE}
````

Output after activating the hook.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# The current chunk hook may not be the default, and should be
# processed prior to doing the numbering; which should come last.

previous_chunk_hook <- knitr::knit_hooks$get("chunk")

knitr::knit_hooks$set("chunk"=function(x,options){
  
  x <- previous_chunk_hook(x, options)
  
  if( isTRUE( options$number ) ) {
    
    str <- "{.r .number"
    if( !is.null(options$startFrom) ) {
      str <- paste0( str, " startFrom=\"", options$startFrom,"\"")
    }
    str <- paste0(str, "}")
    
    x <- gsub( "(\\s?[`]{3,})r", paste0( "\\1", str ), x)

  }

  return( gsub( "(^\n|\n+$)", "", x ) )

})

Numbered Literal chunk with startFrom.

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
````{r default_chunk_hook, eval=FALSE, echo=FALSE, number=TRUE, startFrom="0"}

# The current chunk hook may not be the default, and should be
# processed prior to doing the numbering; which should come last.

previous_chunk_hook <- knitr::knit_hooks$get("chunk")

knitr::knit_hooks$set("chunk"=function(x,options){
  
  x <- previous_chunk_hook(x, options)
  
  if( isTRUE( options$number ) ) {
    
    str <- "{.r .number"
    if( !is.null(options$startFrom) ) {
      str <- paste0( str, " startFrom=\"", options$startFrom,"\"")
    }
    str <- paste0(str, "}")
    
    x <- gsub( "(\\s?[`]{3,})r", paste0( "\\1", str ), x)

  }

  return( gsub( "(^\n|\n+$)", "", x ) )

})
````