簡體   English   中英

將Rscript與.Rnw文件和commandArgs()一起使用

[英]Using Rscript with an .Rnw file AND commandArgs()

我有一個.Rnw文件,包含R代碼塊和LaTeX命令。 到目前為止,我一直在Rstudio中開發和測試此代碼:單擊“編譯PDF”以獲取一些輸出文件和生成的PDF文件。

我現在想使用commandArgs()來為我的.Rnw提供一個參數:一個YAML文件。 此文件包含腳本所需的各種參數。

給出一些背景:

  • 我的.Rnw腳本是一個管道腳本,旨在盡可能“通用”。

  • 我的.Rnw腳本每次都需要許多參數 - 這些參數將在每個項目之間變化。 為了提供這些參數,我目前(已經)使用YAML配置文件。 這意味着我的.Rnw腳本所做的第一件事是導入YAML文件,然后它開始執行操作。

我的問題是:我可以一起使用“Rscript”,“commandArgs()”和knitr。 我希望我的addind一些commandArgs()到我的.Rnw腳本,我將能夠運行所有東西(即在命令行上提供參數以提供YAML文件並編譯PDF),如下所示:

Rscript script.Rnw params.yaml

但是,我收到了關於“\\”的錯誤,我強烈懷疑這與我正在使用.Rnw文件(其中第一件事是LaTeX命令)有關。 然后我看到其他帖子的潛在解決方案,例如:

 Rscript -e "library(knitr); knit('script.Rnw')"
 pdflatex script.tex

但是,這也失敗了 - 我想這並不奇怪,因為我沒有給它配置YAML文件。

我意識到我的設計可能存在缺陷:通過一起使用commandAgrs()和knitr,我使事情變得非常復雜。 我也意識到knitr可能沒有真正設計用於為用作管道的腳本制作報告(至少這是我的印象)。 我想要使​​用它的原因是,對於每個項目,可以生成包含所有結果的快速PDF。

我很感激任何建議。 以下是我的代碼示例:

\documentclass[12pt, a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\hypersetup{
colorlinks   = true, %Colours links instead of ugly boxes
urlcolor     = blue, %Colour for external hyperlinks
linkcolor    = blue, %Colour of internal links
citecolor   = blue %Colour of citations
 }
\usepackage{caption}
\setlength{\parindent}{0pt}
\usepackage{authblk}
\usepackage[nomarkers, nolists]{endfloat} %Positions figures at the end of the document and add no list of names (requires that chunk have fig.cap option)
\usepackage{soul} % Allows underline lines to be broken (use \ul{} instead of \underline{})

\usepackage{helvet} %Set up Arial as font
\renewcommand{\familydefault}{\sfdefault}

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}

\title{\textbf{Report}}
\author{Author}
\date{\today}



\begin{document}

\maketitle

\begingroup
\hypersetup{linkcolor=black} % force independent link colours in table of contents
\tableofcontents
\endgroup

\begingroup
\hypersetup{linkcolor=black} % force independent link colours in list of figures
\listoffigures
\endgroup



\newpage
\section{Introduction} 
This report blah blah blah

\newpage
\section{Results}

<<importing-lib, echo=FALSE, message=FALSE, cache=TRUE>>=

###################################################
# Obtain command-line YAML file and set directory #
###################################################

#!/usr/bin/env Rscript
args= commandArgs(trailingOnly=TRUE)

if (length(args) == 0) {
 stop("this script requires a configuration file (.yaml) as input")

 } else if (length(args) > 1) {
  stop("this script requires only one input: a configuration file (.yaml)")

}

params <-  yaml.load_file(args[1])

setwd(params$workdir)

if (dir.exists(file.path(params$workdir, "results")) == FALSE) {
  dir.create(file.path(params$workdir, "results","edgeR"), recursive = TRUE)
  dir.create(file.path(params$workdir, "results", "gsea", "input_files"), recursive = TRUE)
}

print("Hello!")
@

\end{document}

好的,我找到了兩種似乎有效的方法:

方法1:使用命令行knitr()和pdflatex

在這種情況下,一切都是一個腳本(稱為script.Rnw),但它看起來像這樣:

\documentclass[12pt, a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\hypersetup{
colorlinks   = true, %Colours links instead of ugly boxes
urlcolor     = blue, %Colour for external hyperlinks
linkcolor    = blue, %Colour of internal links
citecolor   = blue %Colour of citations
 }
\usepackage{caption}
\setlength{\parindent}{0pt}
\usepackage{authblk}
\usepackage[nomarkers, nolists]{endfloat} %Positions figures at the end of the document and add no list of names (requires that chunk have fig.cap option)
\usepackage{soul} % Allows underline lines to be broken (use \ul{} instead of \underline{})

\usepackage{helvet} %Set up Arial as font
\renewcommand{\familydefault}{\sfdefault}

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}

\title{\textbf{Test report}}
\date{\today}



\begin{document}

\maketitle

\begingroup
\hypersetup{linkcolor=black} % force independent link colours in table of contents
\tableofcontents
\endgroup

\begingroup
\hypersetup{linkcolor=black} % force independent link colours in list of figures
\listoffigures
\endgroup



\newpage
\section{Introduction} 
This is the introduction.

\newpage
\section{Results}

This is the results.

<<importing-lib, echo=FALSE, message=FALSE>>=

###################################################
# Obtain command-line YAML file and set directory #
###################################################

#!/usr/bin/env Rscript
args= commandArgs(trailingOnly=TRUE)

if (length(args) == 0) {
  stop("this script requires a configuration file (.yaml) as input")

  # } else if (length(args) > 1) {
  # stop("this script requires only one input: a configuration file (.yaml)")

}

library(yaml)
params <- yaml.load_file(args[1])

dir <- getwd()

if (dir.exists(file.path(dir, "results")) == FALSE) {
  dir.create(file.path(dir, "results","part1"), recursive = TRUE)
  dir.create(file.path(dir, "results", "part2", "input_files"), recursive = TRUE)
}

print("Hello!")
print(params$project)
@



\newpage
\section{End}

This is the end!

\end{document}

要運行管道:

Rscript -e "library(knitr); knit('script.Rnw')" params.yaml && pdflatex script.tex

方法2:將代碼分離為.Rnw文件和.R啟動文件

script.Rnw看起來像這樣:

\documentclass[12pt, a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\hypersetup{
colorlinks   = true, %Colours links instead of ugly boxes
urlcolor     = blue, %Colour for external hyperlinks
linkcolor    = blue, %Colour of internal links
citecolor   = blue %Colour of citations
 }
\usepackage{caption}
\setlength{\parindent}{0pt}
\usepackage{authblk}
\usepackage[nomarkers, nolists]{endfloat} %Positions figures at the end of the document and add no list of names (requires that chunk have fig.cap option)
\usepackage{soul} % Allows underline lines to be broken (use \ul{} instead of \underline{})

\usepackage{helvet} %Set up Arial as font
\renewcommand{\familydefault}{\sfdefault}

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}

\title{\textbf{Test report}}
\date{\today}



\begin{document}

\maketitle

\begingroup
\hypersetup{linkcolor=black} % force independent link colours in table of contents
\tableofcontents
\endgroup

\begingroup
\hypersetup{linkcolor=black} % force independent link colours in list of figures
\listoffigures
\endgroup



\newpage
\section{Introduction} 
This is the introduction.

\newpage
\section{Results}

This is the results.

<<first-chunk, echo=FALSE, message=FALSE>>=

print("Hello!")
print(params$project)
@



\newpage
\section{End}

This is the end!

\end{document}

launch.R腳本如下所示:

#!/usr/bin/env Rscript

library(yaml)
library(knitr)

args= commandArgs(trailingOnly=TRUE)

if (length(args) == 0) {
  stop("this script requires a configuration file (.yaml) as input")

  # } else if (length(args) > 1) {
  # stop("this script requires only one input: a configuration file (.yaml)")

}

params <- yaml.load_file(args[1])

dir <- getwd()

if (dir.exists(file.path(dir, "results")) == FALSE) {
  dir.create(file.path(dir, "results","edgeR"), recursive = TRUE)
  dir.create(file.path(dir, "results", "gsea", "input_files"), recursive = TRUE)
}

knit2pdf('script.Rnw')

要運行腳本:

Rscript launch.R params.yaml

方法之間的一個區別是:方法1生成更多文件(* tex,* toc,* out,* log,* aux),大概是因為它在技術上是2個命令。 方法2僅生成.tex和.pdf文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM