加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

更改多语言knitr / RMarkdown文档中的提示

发布时间:2020-12-15 22:04:02 所属栏目:安全 来源:网络整理
导读:我正在编写一个显示bash命令和R命令的.Rmd文件.有没有办法区分具有R代码的块与具有bash代码的块?有一个knitr chunk选项可以将R命令提示符插入一个块中 ```{R,prompt = "true"}plot(rnorm(100))``` 变 plot(rnorm(100)) 但对于bash大块这个 ```{bash,prompt
我正在编写一个显示bash命令和R命令的.Rmd文件.有没有办法区分具有R代码的块与具有bash代码的块?有一个knitr chunk选项可以将R命令提示符插入一个块中

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

> plot(rnorm(100))

但对于bash大块这个

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

变成了这个

> pandoc --version

当我更喜欢这个

$pandoc --version

解决方法

你可以尝试一个简单的钩子:

---
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

并且您可以添加opts_chunk $set(prompt = TRUE),这样您就不必为每个块继续写提示= TRUE

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读