r - 在 RMarkdown 中使用用户输入重复块
问题描述
我正在尝试制作一个自动化脚本来分析 Sanger 测序数据。我的最终目标是将我的 R 脚本转换为可编织的 R Markdown 文件,显示色谱图等等。我目前在过渡到 R Markdown 文件时挂断的是在重复循环中使用用户输入。我知道在编织时 readline 命令不起作用,因为这不是一个交互式过程。据我了解,在编织时接收用户输入的唯一方法是使用参数。但是,我当前脚本的性质是,用户指定正在分析的文件数量,然后决定执行某些循环的次数。可以想象,缺少用户输入会导致无限循环。这是我当前脚本中的一个示例,在 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
}
}
正如你可能看到的,这个块重复的次数完全取决于用户输入。我的大问题是是否有可能使用参数在 RMarkdown 中完成相同的任务(允许用户指定要使用的文件数量并根据该数量选择要使用的文件),或者我是否必须牺牲一些自动化有利于用户定制。是否可以根据其他参数的输入在 RMarkdown 中创建参数?让我知道是否需要清理并提前感谢!!!
解决方案
扩展我的评论,如果您不需要交互性rmarkdown::render
,显然您也可以将所选参数传递给常规 R 脚本。
这是一个简单的示例,将参数传递x
给fn
报告:
这是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")
```
这是在 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())
)
推荐阅读
- flutter - ChangeNotifier 应该使用“extends”还是“with”关键字?- 颤振
- html - 如何使用模态在表中显示数据库中的数据
- java - 使用 JGit 在 Java 中克隆 repo 时出现 AccessDeniedException
- python - 简单问题的 TF 梯度返回零
- google-apps-script - 自动将 CSV 从 googlesheet 导出到我们的服务器
- error-handling - 从 Python 3.4 升级到 3.7 后如何使用 pymssql?无法删除错误
- jaxb - 是否有相当于 codehaus 的插件的
对于 maven-jaxb2-plugin? - sms - Nexmo 不发送短信到美国号码,但可以发送到印度号码
- c# - OpenTK 中的立方体贴图
- javascript - 为什么我的功能没有被识别?JavaScript