我的.Rmd文件周围的包装器,以获取输入路径和输出文件的名称

时间:2018-12-02 20:02:30

标签: r bash sh

我在Rmarkdown中写了几百行代码。我希望该脚本通过包装程序从用户获取输入路径和输出文件的名称。我对这种编码非常陌生,我的问题是我如何拥有一个包装器,将这两个输入内容解析为我的.Rmd文件。

作为示例,这是我的.Rmd文件的前几行。

---
title: "QC Report"
author: "Angelo"
output: 
  html_document:
  css: style.css
  toc: true
  fontsize: 15pt
---

  For the current QC report we first refer to the 10x QC reports for initial sequence and mapping quality metrics.


#1. Loading of libraries 


```{r include = T}
suppressPackageStartupMessages(library(scater))
suppressPackageStartupMessages(library(mvoutlier))
suppressPackageStartupMessages(library(Rtsne))
suppressPackageStartupMessages(library(limma))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(repr))
suppressPackageStartupMessages(library(cowplot))
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(rmarkdown))
options(stringsAsFactors = FALSE)
```


#2. Loading expression data

\newline



```{r include = T}

  loadSCE <- function(path){
  sce <- read10XResults(path)
  #sce <- normalize(sce) # Data normalization based on scran

  mitochondrialGenes <- as.character(rowData(sce)[startsWith(rowData(sce)$symbol, "mt-"),]$id)

  isSpike(sce, "MT") <- rownames(sce) %in% mitochondrialGenes

  sce <- calculateQCMetrics(sce, 
                            feature_controls = list(
                              MT =  isSpike(sce, "MT")
                            ))
}

```



```{r include = T}

paths <- list.dirs(path = "/home/mydir/SampleData/", recursive = FALSE)


for (i in 1:length(paths))
  assign(paste0("sce_",i), loadSCE(paths[i]))


sce=0
for (i in 1:length(paths))
 sce[i]<-print(noquote(paste0("sce_",i)))

```

```{r include = T}
    t_list <- mget(ls(pattern="sce_\\d+"))
     for(i in seq_along(t_list))
 {
 metadata(t_list[[i]])["name"] <- paste0("iMates-",i)
}
```

本质上,我想要的是这样的东西:

wrapper.sh -path /home/mydir/  -outfile output.html

or 

Raw.Rmd -p /home/mydir/  -outfile output.html

请帮助我。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用R脚本,使用Rscript script.R /home/mydir/ output.html之类的代码(或在某些操作系统上使用如下所示的hash-bang脚本)运行这些事情。

文件script.R应该包含用于从命令行获取参数并将其传递给rmarkdown::render的代码。可以做到:

#! /path/to/Rscript --vanilla --default-packages=utils,rmarkdown
args <- commandArgs(TRUE)
path <- args[1]
outfile <- args[2]

render("doc.Rmd", output_file = outfile, params = list(path = path))

这将doc.Rmd固定为文档名称。需要声明path是一个参数;将此添加到您的YAML标头中:

params: 
  path: "."

如果您未指定路径,则字符串"."是默认值。

要从参数中检索路径,可以像这样执行代码块:

```{r}
path <- params$path
```

然后只需像在代码块中使用任何其他变量一样使用path