Using knitting in a markdown file, but ignoring a piece of code - sourcing knitr files

This is similar to How to fix the R Markdown file, for example `source ('myfile.r')`? , with the following difference: I want my markup file to generate an R script for a future source. The previous question was interested in a direct search for the markdown file (which at the moment does not seem simple).

You can enable compilation of the .Rmd markup file that generates a useful R source file by including the following:

```{r, ignore = TRUE, include = FALSE}
knit('markdown_file.Rmd', 'source_file.R', tangle = T)
```

This creates the source_file.R file perfectly, but it contains a call knitat the end. So when I'm source("source_file.R)in another script, it recreates and rewrites itself, which seems like bad practice. Is there anyway to say knitignore the code snippet in the .Rmd file?

+4
source share
1 answer

I solved this with a bash script and completely removing this dream piece from the R script.

Rscript -e 'knitr::knit("markdown_file.Rmd", "source_file.R", tangle = T)'

Thus, with this approach, I could use a similar bash script to embed my .Rmd into the desired documentation output (for example, pdf, code not shown), then this line of code to create an R file, which can be used by other R scripts. In fact, I remove the need to call knitin the Rmd file (or inside RStudio).

+1
source

Source: https://habr.com/ru/post/1570234/


All Articles