如何根据参数只运行R markdown文件的一部分?

问题描述 投票:0回答:1

我试图根据参数的值仅运行 R markdown 文件的特定部分。例如,在这个改编自 R markdown 典型示例的 R markdown 文件中(我没有在示例中使用“```”符号关闭 r 块,因为它关闭了 StackOverflow 问题中的代码区域):

---
title: "Untitled"
date: "`r Sys.Date()`"
output: pdf_document
params: 
  CategReport:
    label: "Report with plots?"
    value: Choose type of report
    input: select
    multiple: true
    choices: [Plots, Text]
---

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


## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)


if Plots %not in% params$CategReport, don't run the area from below, continue after it ->

{ ### PLOT AREA START

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)

### PLOT AREA END}

## Other stuff that will be printed regardless if plot area is skipped or not

我想要的是,如果用户在使用参数编织时在多项选择问题中选择“绘图”选项,则将显示“if Plots %in% params$CategReport,从下面运行所有内容 ->”下面的所有内容,包括代码块之外的文本。

我在网上看到了一些选项来包含基于参数值的条件代码块,但如果我这样做,我只能包含代码块中的文本。如果我在代码块中包含文本,我不知道如何访问所有漂亮的 R markdown 格式,如“##”或“**”以及页面上漂亮的文本方向。如果我在代码块中执行此操作,它将显示为代码注释。

因此,我正在考虑设置一个条件,如果不满足,将跳过报告的整个部分,但仍然显示该部分下方的降价部分(例如“##其他将打印的内容”)是否跳过绘图区域').

基本上,我正在寻找类似的内容:如果满足条件,则跳过或不跳过降价的这一部分 - 如果未跳过,则正常打印,如果跳过,则打印本部分之前和之后的所有内容(最重要的是,这部分包括代码块外部的内容和代码块内部的内容)。

这样的东西可以吗?

r r-markdown markdown
1个回答
0
投票

一种选择是使用子文档并将条件传递给

eval=
块选项,以便仅当用户选择
"Plots"
:

时才会呈现子文档
---
date: "`r Sys.Date()`"
output: pdf_document
params: 
  CategReport:
    label: "Report with plots?"
    value: Choose type of report
    input: select
    multiple: true
    choices: [Plots, Text]
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

```{r eval="Plots" %in% params$CategReport, child = "child.Rmd"}
```

Other stuff that will be printed regardless if plot area is skipped or not

孩子.Rmd

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```
© www.soinside.com 2019 - 2024. All rights reserved.