我有一个.Rnw文件,包含R代码块和LaTeX命令。到目前为止,我一直在Rstudio中开发和测试这段代码:单击“编译PDF”获得一些输出文件和生成的PDF文件。
现在我想使用commandArgs()来为.Rnw提供一个参数: YAML文件。此文件包含脚本所需的各种参数。
提供上下文的:
我的问题是:我是否可以同时使用"Rscript“、"commandArgs()”和编织品。我希望我将一些commandArgs()添加到我的.Rnw脚本中,我将能够运行所有东西(即在命令行上给出参数以提供YAML文件并编译.Rnw),如下所示:
Rscript script.Rnw params.yaml但是,我得到了关于"\“的错误,我强烈怀疑这与我使用.Rnw文件(以及其中的第一个LaTeX命令)有关。然后,我在其他职位上看到了可能的解决办法,例如:
Rscript -e "library(knitr); knit('script.Rnw')"
pdflatex script.tex但是,这也失败了--我想这并不奇怪,因为我还没有给它配置YAML文件。
我意识到我的设计可能有缺陷:通过使用commandAgrs()和编织机,我使事情变得相当复杂。我还意识到,编织机可能还没有真正为用作管道的脚本编写报告(至少这是我的印象)。我想使用它的原因是,对于每个项目,可以生成一个快速的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}发布于 2019-05-31 11:54:26
好的,我找到了两种方法,似乎起作用了:
方法1:使用命令行和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}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只生成一个.tex和一个.pdf文件。
https://stackoverflow.com/questions/56381516
复制相似问题