[英]Using Rscript with an .Rnw file AND commandArgs()
I have an .Rnw file, containing R code chunks and LaTeX commands. 我有一个.Rnw文件,包含R代码块和LaTeX命令。 So far, I have been developing and testing this code in Rstudio: clicking on "compile PDF" to obtain some output files and a PDF file generated.
到目前为止,我一直在Rstudio中开发和测试此代码:单击“编译PDF”以获取一些输出文件和生成的PDF文件。
I would now like to use commandArgs() to be able to give my .Rnw one single argument: a YAML file. 我现在想使用commandArgs()来为我的.Rnw提供一个参数:一个YAML文件。 This file contains all sorts of parameters that are needed by the script.
此文件包含脚本所需的各种参数。
To give some context: 给出一些背景:
My .Rnw script is a pipeline script which was designed to be as "generic" as possible. 我的.Rnw脚本是一个管道脚本,旨在尽可能“通用”。
My .Rnw script requires a number of parameters each time - these will be the parameters varying between each project. 我的.Rnw脚本每次都需要许多参数 - 这些参数将在每个项目之间变化。 To supply these parameters, I currently (already) use a YAML configuration file.
为了提供这些参数,我目前(已经)使用YAML配置文件。 This means the first thing my .Rnw script does is import the YAML file, and then it starts doing things.
这意味着我的.Rnw脚本所做的第一件事是导入YAML文件,然后它开始执行操作。
My problem is: can I used "Rscript", "commandArgs()" and knitr all together. 我的问题是:我可以一起使用“Rscript”,“commandArgs()”和knitr。 I was hoping that my addind some commandArgs() to my .Rnw script, I would be able to run everything (ie give argument on command line to supply YAML file and compile PDF) like so:
我希望我的addind一些commandArgs()到我的.Rnw脚本,我将能够运行所有东西(即在命令行上提供参数以提供YAML文件并编译PDF),如下所示:
Rscript script.Rnw params.yaml
However, I have getting this error about a "\\", which I strongly suspect is something to do with the fact I'm using a .Rnw file (and the very first thing in it a LaTeX command). 但是,我收到了关于“\\”的错误,我强烈怀疑这与我正在使用.Rnw文件(其中第一件事是LaTeX命令)有关。 I then saw potential solution on other posts such as:
然后我看到其他帖子的潜在解决方案,例如:
Rscript -e "library(knitr); knit('script.Rnw')"
pdflatex script.tex
However, this also fails - unsurprisingly I suppose because I haven't given it my configuration YAML file. 但是,这也失败了 - 我想这并不奇怪,因为我没有给它配置YAML文件。
I realise my design is likely flawed: by using commandAgrs() AND knitr together, I am making things quite complicated. 我意识到我的设计可能存在缺陷:通过一起使用commandAgrs()和knitr,我使事情变得非常复杂。 I also realise that knitr may have not be truly designed for making reports for scripts that are used as pipelines (at least that's my impression).
我也意识到knitr可能没有真正设计用于为用作管道的脚本制作报告(至少这是我的印象)。 The reason I want to use it is so that for each project, a quick PDF can be generated with all the results.
我想要使用它的原因是,对于每个项目,可以生成包含所有结果的快速PDF。
I would appreciate any advice. 我很感激任何建议。 Here is a sample of my code:
以下是我的代码示例:
\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}
Ok so I have found 2 ways in which it seems to work: 好的,我找到了两种似乎有效的方法:
Method 1: using command line knitr() and pdflatex 方法1:使用命令行knitr()和pdflatex
In this case, everything is one script (called script.Rnw) still but it looks like so: 在这种情况下,一切都是一个脚本(称为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}
To run the pipeline: 要运行管道:
Rscript -e "library(knitr); knit('script.Rnw')" params.yaml && pdflatex script.tex
Method 2: separating code into a .Rnw file and .R launch file 方法2:将代码分离为.Rnw文件和.R启动文件
The script.Rnw looks like so: 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}
The launch.R script looks like so: 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')
To run the script: 要运行脚本:
Rscript launch.R params.yaml
One difference between the methods: method 1 generates more files (*tex, *toc, *out, *log, *aux), presumably because it's technically 2 commands. 方法之间的一个区别是:方法1生成更多文件(* tex,* toc,* out,* log,* aux),大概是因为它在技术上是2个命令。 Method 2 only generates a .tex and a .pdf file.
方法2仅生成.tex和.pdf文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.