使用 knitr 编译 PDF 时不需要的 R 代码回显
Unwanted R code echo when compiling PDF with knitr
我有一个包含以下代码的 .Rnw 文件。 运行 knitr::knit()
一次,R 代码不回显。 运行 knitr::knit()
第二次,R 代码在 PDF 中回显。为什么?如何防止 R 代码回显?
<<load_chapter_2, echo=FALSE, warning=FALSE, message=FALSE,cache=TRUE>>=
options(digits=2)
opts_chunk$set(eval=TRUE, results = "hide", echo=FALSE, warning=FALSE, message=FALSE, fig.height=5, fig.width=5, fig.pos="!ht", fig.align='center')
@
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum} % Required to insert dummy text
% \usepackage{nameref} commented out as was causing extra \else error
\usepackage{graphicx}
\usepackage{placeins} % to control figure placement with \FloatBarrier
\usepackage{xspace}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{array} % for line breaks in table
\usepackage[comma, sort&compress]{natbib}
\setlength{\bibsep}{0pt plus 0.3ex}
\begin{document}
\title{}
\author{}
\date{\today}
\maketitle
\section{Header}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<<plot_1>>=
plot(1)
@
\FloatBarrier
\end{document}
您必须设置 cache = F
。我认为原因是 cache = TRUE
第二次将代码存储在内存中并且 echo = F
被忽略了。
您不应该缓存您的设置块 - 这意味着您的 options
和 opts_chunk$set
在后续调用 knit()
时不会 运行。 cache = TRUE
应该用于产生结果的代码块,如果代码没有改变,结果也不会改变。但是,您还需要注意依赖关系。
有关详细信息,请参阅 http://yihui.name/knitr/demo/cache/。请特别查看标有 'Important notes' 的部分,其中包含以下内容:
It is extremely important to note that usually a chunk that has
side-effects should not be cached. Although knitr tries to retain the
side-effects from print(), there are still other side-effects that are
not preserved. Here are some cases that you must not use cache for a
chunk:
- setting R options like
options('width')
or pdf.options()
or any other options in knitr like opts_chunk$set()
, opts_knit$set()
and
knit_hooks$set()
- loading packages via
library()
in a cached chunk and these packages will be used by uncached chunks (it is entirely OK to load
packages in a cached chunk and use them only in cached chunks because
knitr saves the list of packages for cached chunks, but uncached
chunks are unable to know which packages are loaded in previous cached
chunks)
Otherwise next time the chunk will be skipped and all the settings in
it will be ignored. You have to use cache=FALSE
explicitly for these
chunks.
我有一个包含以下代码的 .Rnw 文件。 运行 knitr::knit()
一次,R 代码不回显。 运行 knitr::knit()
第二次,R 代码在 PDF 中回显。为什么?如何防止 R 代码回显?
<<load_chapter_2, echo=FALSE, warning=FALSE, message=FALSE,cache=TRUE>>=
options(digits=2)
opts_chunk$set(eval=TRUE, results = "hide", echo=FALSE, warning=FALSE, message=FALSE, fig.height=5, fig.width=5, fig.pos="!ht", fig.align='center')
@
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum} % Required to insert dummy text
% \usepackage{nameref} commented out as was causing extra \else error
\usepackage{graphicx}
\usepackage{placeins} % to control figure placement with \FloatBarrier
\usepackage{xspace}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{array} % for line breaks in table
\usepackage[comma, sort&compress]{natbib}
\setlength{\bibsep}{0pt plus 0.3ex}
\begin{document}
\title{}
\author{}
\date{\today}
\maketitle
\section{Header}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<<plot_1>>=
plot(1)
@
\FloatBarrier
\end{document}
您必须设置 cache = F
。我认为原因是 cache = TRUE
第二次将代码存储在内存中并且 echo = F
被忽略了。
您不应该缓存您的设置块 - 这意味着您的 options
和 opts_chunk$set
在后续调用 knit()
时不会 运行。 cache = TRUE
应该用于产生结果的代码块,如果代码没有改变,结果也不会改变。但是,您还需要注意依赖关系。
有关详细信息,请参阅 http://yihui.name/knitr/demo/cache/。请特别查看标有 'Important notes' 的部分,其中包含以下内容:
It is extremely important to note that usually a chunk that has side-effects should not be cached. Although knitr tries to retain the side-effects from print(), there are still other side-effects that are not preserved. Here are some cases that you must not use cache for a chunk:
- setting R options like
options('width')
orpdf.options()
or any other options in knitr likeopts_chunk$set()
,opts_knit$set()
andknit_hooks$set()
- loading packages via
library()
in a cached chunk and these packages will be used by uncached chunks (it is entirely OK to load packages in a cached chunk and use them only in cached chunks because knitr saves the list of packages for cached chunks, but uncached chunks are unable to know which packages are loaded in previous cached chunks)Otherwise next time the chunk will be skipped and all the settings in it will be ignored. You have to use
cache=FALSE
explicitly for these chunks.