Knitr: Do Not Wipe A Piece

Consider the following test.Rmd:

```{r setup, purl=FALSE}
opts_chunk$set(purl=FALSE)
opts_template$set(nopurl = list(purl=FALSE))
```     

```{r test1}
print(1)
```

```{r test2, opts.label='nopurl'}
print(2)
```

```{r test3, purl=FALSE}
print(3)
```

purl('test.Rmd')gives test.Rwhere none of the test * pieces are supposed, but:

## ----test1---------------------------------------------------------------
print(1)


## ----test2, opts.label='nopurl'------------------------------------------
print(2)

Just test3do not smooth, the rest wear out, despite the global option opts_chunk$set(purl=FALSE)and label nopurl.
Why?

+4
source share
2 answers

Based on Yihui feedback, the way to go is as follows:

```{r setup, purl=FALSE}
knit_hooks$set(purl = hook_purl)
opts_template$set(nopurl = list(purl=FALSE))
opts_template$set(dopurl = list(purl=TRUE))
```

```{r test}
print(1)
```

```{r test2, opts.label='nopurl'}
print(2)
```

```{r test3, opts.label='dopurl'}
print(3)
```

With this approach, you do not need:

purl('test.Rmd') 

You just:

knit('test.Rmd') 

and get both a regular file test.mdand test.R. The latter is as follows:

## ----test----------------------------------------------------------------
print(1)

## ----test3, opts.label='dopurl'------------------------------------------
print(3)

As you can see, when given knit_hooks$set(purl = hook_purl), the default behavior for chunks is shredding.

nopurl dopurl , (no-) .
, :

N.B. , R script, 'purl()' , "knit()" . "Knit()" ( , , , R, "envir" ..). , "knit()" , "knit()" , R script, 'purl()'. , .

knit_hooks$set(purl = hook_purl) purl=FALSE, .

+1

, purl() , . purl() , . ?knitr::hook_purl. , ; (. "" ?knitr::purl).

+2

All Articles