Unwanted R code echo when compiling PDF with knitr

234 views Asked by At

I have an .Rnw file that contains the code below. Run knitr::knit() once and the R code doesn't doesn't echo. Run knitr::knit() a second time and the R code echoes in the PDF. Why? How can I prevent R code from echoing?

<<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}
2

There are 2 answers

2
Nick Kennedy On BEST ANSWER

You shouldn't cache your setup block - this means that your options and opts_chunk$set won't be run on subsequent calls of knit(). cache = TRUE should be used for blocks of code that produce results where if the code hasn't changed, the results won't have changed either. However, you also need to be careful of dependencies.

See http://yihui.name/knitr/demo/cache/ for more detail. Look in particular at the section labelled 'Important notes' which has the following:

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:

  1. 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()
  2. 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.

0
SabDeM On

You have to set cache = F. I think the reason is that with cache = TRUE the second time the code is stored in memory and the echo = F is ignored.