Skip to content

Instantly share code, notes, and snippets.

@aoles
Created October 8, 2018 10:09
Show Gist options
  • Save aoles/bf4ce1c881c6dcda4ac1833b20de1ed1 to your computer and use it in GitHub Desktop.
Save aoles/bf4ce1c881c6dcda4ac1833b20de1ed1 to your computer and use it in GitHub Desktop.
Save bash code chunks in R Markdown documents into separate script files
---
output: html_document
---
```{r setup, include=FALSE}
knit_hooks_source <- knitr::knit_hooks$get("source")
knitr::knit_hooks$set(source = function (x, options) {
if (!is.null(options$save))
writeLines(x, options$save)
knit_hooks_source(x, options)
})
knitr::opts_hooks$set(engine = function(options) {
if (options$engine == "bash") {
options$eval <- FALSE
options$save <- paste0(options$label, ".sh")
}
options
})
```
```{bash hello}
#!/bin/bash
echo "Hello World!"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment