Editing an invitation in a multilingual knitr / RMarkdown document

I am writing a file .Rmdthat displays bash commands and R commands. Is there a way to differentiate chunks with R code from code with bash code? There's a chitrn option that inserts the R command line into a piece so that

```{R, prompt = "true"}
plot(rnorm(100))
```

becomes

> plot(rnorm(100))

but for bash pieces it's

```{bash, prompt = "true"}
pandoc --version
```

becomes this

> pandoc --version

when i would prefer it

$ pandoc --version
+4
source share
2 answers

You can try a simple hook:

---
output: html_document
---

```{r}
library('knitr')
knit_hooks$set(
  prompt = function(before, options, envir) {
    options(prompt = if (options$engine %in% c('sh','bash')) '$ ' else 'R> ')
})
```

```{r, prompt=TRUE}
1+1
```

but for the bash chunks this

```{bash, prompt=TRUE}
pandoc --version | head -1
```

```{r, prompt=TRUE}
1+1
```

enter image description here

And you can add opts_chunk$set(prompt=TRUE), so you do not need to write prompt=TRUEfor each fragment

+6
source

This is a bit awkward, but I just realized that I could “switch” the prompt:

```{r, echo = F}
options(prompt = "$ ")
```

```{bash, eval = F, prompt = T}
pandoc --version
```

```{r, echo = F}
options(prompt = "> ")
```

```{r, eval = F, prompt = T}
plot(rnorm(100))
```

which gives

$ pandoc --version
> plot(rnorm(100))
+2

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


All Articles