【问题标题】:function to generate both markdown and r code? [duplicate]生成markdown和r代码的函数? [复制]
【发布时间】:2021-07-29 01:29:22
【问题描述】:

我有这个:

# Title 1

'``{r}
barplot(mtcars$mpg)

'``
text describing plot 1

# Title 2

'``{r}
barplot(mtcars$mpg)

'``
text describing plot 2

# Title 3

'``{r}
barplot(mtcars$mpg)

'``
text describing plot 3

如你所见,重复的单位是:

# Title x

'``{r}
barplot(mtcars$mpg)

'``
text describing plot x

有没有办法创建一个生成这个重复单元的函数?

预期的输入等于我上面的输入:

'``{r}
function_that_creates_title_graph_text(1)

function_that_creates_title_graph_text(2)

function_that_creates_title_graph_text(3)
'``

【问题讨论】:

标签: r r-markdown


【解决方案1】:

您可以将长字符串作为变量,在 Rmarkdown 的内联 R 代码中显示 R 值,并使用 `r your_r_c​​ode_here`。

```{r }
longstring <- 'We random sampled ' 
random <-  runif(1, 1, 99)
cat(paste(longstring, random, "units"))
```

您可以将其转换为内联 R 代码,因为变量是在之前的块中创建的,像这样

`r paste0(longstring, random, ' units')`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2017-02-18
    • 2014-03-10
    • 2013-04-20
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多