简体   繁体   English

在 RMarkdown 中使用用户输入重复块

[英]Repeating chunks with user input in RMarkdown

I am attempting to make an automated script to conduct analysis of Sanger Sequencing data.我正在尝试制作一个自动化脚本来分析 Sanger 测序数据。 My end goal is to convert my R script into a knittable R Markdown file displaying chromatograms and what not.我的最终目标是将我的 R 脚本转换为可编织的 R Markdown 文件,显示色谱图等等。 My current hang up in the transition to an R Markdown file is in using user input in repeating loops.我当前在转换到 R Markdown 文件时挂断是在重复循环中使用用户输入。 I'm aware that the readline command doesn't work while knitting as this is not an interactive process.我知道在编织时 readline 命令不起作用,因为这不是一个交互式过程。 From what I understand, the only way to receive user input while knitting is through the use of parameters.据我了解,在编织时接收用户输入的唯一方法是使用参数。 However, the nature of my current script is such that the user specifies how many files are being analyzed which then decides how many times certain loops are executed.但是,我当前脚本的性质是,用户指定正在分析多少文件,然后决定执行某些循环的次数。 As one might imagine, lacking user input results in infinite loops.可以想象,缺少用户输入会导致无限循环。 This is an example from my current script that runs just fine in RStudio:这是我当前脚本中的一个示例,在 RStudio 中运行良好:

# install.packages("BiocManager")
# BiocManager::install("sangerseqR")
# BiocManager::install("Biostrings")

library(BiocManager)
library(sangerseqR)
library(Biostrings)

refnum <- readline(prompt = "Enter Number of Reference Files to be aligned: ")
refnum <- as.integer(refnum)
c <- 1
repeat{
  # Compiles reference files
  repeat{
    wtreffile <- readline(prompt = paste("Enter WT Reference File ", c, ": ", sep = ""))
    if (!file.exists(wtreffile)) {
      message("File Does Not Exist. Please Re-Enter.")
    } else {
      break
    }}
  repeat{
    insreffile <- readline(prompt = paste("Enter INS/DEL/MUT Reference File ", c, ": ", sep = ""))
    if (!file.exists(insreffile)) {
      message("File Does Not Exist. Please Re-Enter.")
    } else {
      break
    }}
  # Reads sequence from reference files and writes it as a DNAString object to be used for alignment
  wt <- readDNAStringSet(wtreffile, format = "fasta", nrec = 1L, skip = 0L, seek.first.rec = TRUE)
  ins <- readDNAStringSet(insreffile, format = "fasta", nrec = 1L, skip = 0L, seek.first.rec = TRUE)
  # Aligns wild type and insert reference files pairwise
  pa <-pairwiseAlignment(pattern = ins, subject = wt)
  pafile <- readline(prompt = paste("Enter name for reference alignment of ", wtreffile, " and ", insreffile, ": ", sep = ""))
  writePairwiseAlignments(pa, file = pafile)
  c <- c + 1
  if (c > refnum / 2) {
    break
  }
}

As you can probably see, the amount of times this chunk repeats is entirely dependent on user input.正如你可能看到的,这个块重复的次数完全取决于用户输入。 My big question is whether its possible to accomplish this same task (allowing the user to specify the amount of files to be used and select which files to use based on that amount) in RMarkdown using parameters, or if I'll have to sacrifice some automation in favor of user customization.我的大问题是是否有可能在 RMarkdown 中使用参数完成相同的任务(允许用户指定要使用的文件数量和 select 使用哪些文件),或者我是否必须牺牲一些自动化有利于用户定制。 Is it possible to create parameters in RMarkdown based on the input of other parameters?是否可以根据其他参数的输入在 RMarkdown 中创建参数? Let me know if anything needs cleared up and thanks in advance!!!让我知道是否需要清理并提前感谢!!!

Expanding on my comment, if you don't need interactivity, you can also pass the chosen parameters to rmarkdown::render in a regular R script, obviously.扩展我的评论,如果您不需要交互性,显然您也可以在常规 R 脚本中将所选参数传递给rmarkdown::render

Here is a simple example, passing the parameters x and fn to the report:这是一个简单的示例,将参数xfn传递给报告:

This is the report file report.rmd saved in the working directory:这是保存在工作目录中的报告文件report.rmd

title: "Report"
output: pdf_document
params:
  x: NA
  fn: NA
---

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

## Plot with parameters passed from R script

The red line shows the `r params$fn` of the distribution.

```{r plot, echo=FALSE}
x <- params$x
fn <- params$fn

set.seed(1)
df <- rnorm(100)
plot(density(df))
if(x!="NA") abline(v=x, col="red") 

# in this example, it would make more sense to pass the function, not `x`:
# if(x!="NA") abline(v=do.call(fn, list(df)), col="red") 
```

This is where the parameters are passed to the report in an R script:这是在 R 脚本中将参数传递给报告的地方:

tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)

# parameters passed to report:
params <- list(x = 0.3, fn = "mean") 
rmarkdown::render(tempReport, output_file = "~/Downloads/new.report.pdf", run_pandoc=TRUE,
                  params = params, output_format="pdf_document", clean=FALSE,
                  envir = new.env(parent = globalenv())
)

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

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