R Markdown参数-我可以组合向量用作参数吗?

时间:2018-09-18 16:40:30

标签: r parameters parameter-passing r-markdown

如果您在控制台中输入summary(c(mtcars, cars)),您将收到输出。但是下面的R Markdown(如下所示)会引发错误,“找不到对象'test'”。

如何组合向量以用作R Markdown中的参数?也许这是不允许的,并且参数必须是单个数字或字符串?我希望test参数为c(mtcars, cars)。或者,也许将来我会有一个VariableX参数,我想成为c("apple", "pear", "orange")

---
title: "Untitled"
output: html_document
params:
  n: 100
  test: c(mtcars, cars)
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r test}
summary(test)
```

1 个答案:

答案 0 :(得分:3)

R Markdown book中所述:

  

我们还可以通过在R表达式之前包含!r来使用R对象

使用Dat <- structure(list(IndIDII = c("BHS_376", "BHS_376", "BHS_376", "BHS_376", "BHS_378", "BHS_378", "BHS_378", "BHS_391", "BHS_391", "BHS_394", "BHS_394", "BHS_394", "BHS_395", "BHS_395", "BHS_395", "BHS_395" ), Year = c("2015", "2016", "2017", "2018", "2015", "2016", "2017", "2015", "2016", "2016", "2017", "2018", "2015", "2016", "2017", "2018"), MigStratFact = structure(c(3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 2L), .Label = c("Resident", "ShortDist", "MidDist", "LongDist"), class = "factor")), class = "data.frame", row.names = c(NA, -16L)) 访问报告中的参数。放在一起:

params$test

enter image description here