简体   繁体   中英

Using knitr to create HTML slides and separate R code file

Following on from this question ... I am not sure where to set knitr option if I want to output a separate file of R code. The following does not provide the expected additional .R files in my working directory.

---
output: ioslides_presentation
---

```{r setup, include=FALSE}
library("knitr"); purl("myfile.rmd")
#library("knitr"); knit("test_tangle.Rmd", tangle = TRUE)
#opts_knit$set(tangle=TRUE)
```

## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```

but an error message...

Quitting from lines 6-7 (myfile.rmd) 
Error in readLines(if (is.character(input2)) { : 
  cannot open the connection
Calls: <Anonymous> ... withVisible -> eval -> eval -> purl -> knit ->     readLines
Execution halted

I recommend you to use the hook_purl function instead. The function purl() (or equivalently, knit(tangle = TRUE) ) may fail to work in certain cases, and the hook function hook_purl() is more reliable. See ?hook_purl for more information.

---
output: ioslides_presentation
---

```{r setup, include=FALSE}
library("knitr")
knit_hooks$set(purl = hook_purl)
```

## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```

Then as you knit the document, the R script will be automatically generated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM