繁体   English   中英

"基于交互式参数选择运行 R Markdown"

[英]Run an R Markdown based on interactive parameter choice

我正在尝试在 R Markdown 上执行以下操作。 基本上,我希望用户能够在名为“Machine”的参数的两个值之间进行选择,并根据选择的值运行一些块而不是其他块。 我知道“eval”选项在这里可能很有用,但我不知道如何使用它来达到我的目标。 我目前的代码是这样的(在 R Markdown 中):

    ---
title: "SUM and SAM"
output: html_document
params:
  machine:
  label: "Machine"
  value: SUM
  input: select
  choices: [SUM, SAM]
  printcode:
    label: "Display Code:"
    value: TRUE
  date: !r Sys.Date()
  data:
    label: "Input dataset:"
    value: None
    input: file
  years_of_study:
    input: slider
    min: 2018
    max: 2020
    step: 1
    round: 1
    sep: ''
    value: [2018, 2019]

---


```{r setup, include=FALSE}

############# IMPORTANT ################################### 
#Remember to Knit with Parameters here using the "Knit button"--> "Knit with Parameters".
#################################################### 

#If you only want the parameters to be shown, run the following.
#knit_with_parameters('~/Desktop/Papers/git_hub/sum_sam.Rmd')

#This must be left uncommented if we want to show the content of the Markdown file once "Knit with Parameters" is pressed.
knitr::opts_chunk$set(echo = TRUE)

假设有一个名为foo.Rmd<\/code>的文件,其内容如下:

---
title: "SUM and SAM"
output: html_document
params:
  machine: default
---

```{r setup, include = FALSE}
selected <-params$machine
```

  
```{r, eval = selected == "SAM", echo=FALSE}
print("SAM was chosen")
```

  
```{r, eval =selected == "SUM", echo=FALSE}
print("SUM was chosen")
```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM