Yaming
Yaming

Reputation: 97

Load R packages when writing report with Knitr and LaTex

I have the below combined systems for writing report with Knitr and LaTeX,

  1. Rstudio Version 0.99.473 with R version 3.2.2
  2. Knitr_1.11
  3. MiKTeX 2.9
  4. Windows 7

An example .Rnw code is shown below, my issue is that a text string "latticeknitrstatsgraphicsgrDevicesutilsdatasetsmethodsbase" was created right underneath "Figure 1: Test". I wonder how to get rid of it.

\documentclass[letterpaper]{article}
\title{An example}
\author{Me}
\date{\today{}}

<<setup, include=FALSE, cache=FALSE>>=
knit_hooks$set(myPackage = function(before, options, envir){
  if(before)
    library(lattice)  else NULL
})
@

\begin{document}
\maketitle
\newpage
\section{My section 1}
\begin{figure}
\caption{Test}\label{fig:GofBase}
<<myPackage=TRUE, echo=FALSE, results='asis', cache=TRUE, fig.show='hold', fig.align='center', warning=FALSE>>=
xyplot(rnorm(100)~rnorm(100))
@
\end{figure}

\end{document}

Upvotes: 2

Views: 451

Answers (2)

CL.
CL.

Reputation: 14987

As the question is not very clearly formulated, and the minimal example is not absolutely "minimal", here a improved version:

Question

Why does the following minimal example add a string like latticeknitrstatsgraphicsgrDevicesutilsdatasetsmethodsbase to the output?

\documentclass{article}

<<setup>>=
knit_hooks$set(myPackage = function(before, options, envir){

    if(before) library(lattice)
})
@

\begin{document}
<<myPackage=TRUE>>=
xyplot(rnorm(100)~rnorm(100))
@
\end{document}

Answer

This has nothing to do with lattice and it's not a special feature of knitr chunk hooks.

What happens?

When a chunk hook returns a character value, this value is included in the output:

In knitr, hooks can also be used to insert texts into the output. To do this, the hook function must return a character result. [knitr: hooks]

In this case, there is no explicit return value of the hook. Therefore, the return value of if() is returned. if() in turn returns the value that library returns:

if returns the value of the expression evaluated, or NULL invisibly if none was (which may happen if there is no else). [see ?"if"]

And finally:

library returns (invisibly) the list of attached packages [see ?libray]

How to avoid it?

First of all, the example is not very good. There's no reason to load a package using a chunk hook. If lattice is required, it should be loaded in the setup chunk.

But there may be cases where a similar chunk hook is useful. Then, the solution is to explicitly return NULL:

knit_hooks$set(myPackage = function(before, options, envir){

    if(before) library(lattice)
    return(NULL)
  })

Upvotes: 1

Dirk is no longer here
Dirk is no longer here

Reputation: 368439

Here is a minimally changed version that "works for me" (also from RStudio)

\documentclass[letterpaper]{article}
\title{An example}
\author{Me}
\date{\today{}}

<<setup, include=FALSE, cache=FALSE, echo=FALSE>>=
knit_hooks$set(myPackage = function(before, options, envir){
  if(before)
    library(lattice)  else NULL
})
@

\begin{document}
%\SweaveOpts{concordance=TRUE}
\maketitle
\newpage
\section{My section 1}
\begin{figure}
\caption{Test}\label{fig:GofBase}
<<myPackage=TRUE, echo=FALSE, cache=FALSE, fig.show='hold', fig.align='center', warning=FALSE>>=
print(xyplot(rnorm(100)~rnorm(100)))
@
\end{figure}

\end{document}

I added a echo=FALSE to the first chunk, and removed results='asis' from the second chunk.

Upvotes: 0

Related Questions