Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of packages used in a knitr .Rnw document?

Tags:

r

latex

knitr

Using RStudio --> CompilePDF

In a .Rnw document to be processed with pdflatex, I'd like to get a list of all user (me) packages loaded via library() or require() in the document. I tried to use sessionInfo(), as in

   \AtEndDocument{
   \medskip
   \textbf{Packages used}: \Sexpr{names(sessionInfo()$loadedOnly)}.
   }

however, what this prints is just the list of packages used by knitr itself,

Packages used: digest, evaluate, formatR, highr, stringr, tools.

not those I explicitly referred to. I believe this is because knitr runs the code chunks within an internal environment, but I don't know how to access that.

I know about the file cache/__packages that is created with cache=TRUE; is there any way to generate this automatically without caching?

like image 590
user101089 Avatar asked Sep 29 '13 23:09

user101089


3 Answers

Without cache (cache = FALSE), what you want is basically

unique(c(.packages(), loadedNamespaces()))

With cache enabled, it is slightly more complicated, because the package names are cached as well; the second time you compile the document, these packages are not loaded unless you have invalidated the cache. In this case, as you have noticed, there is a file cache/__packages, and you can read the packages names there, so

unique(c(.packages(), loadedNamespaces(), readLines('cache/__packages')))

You may want to make the code more robust (e.g. check if cache/__packages exists first), and exclude certain packages from the list (e.g. knitr and its friends), as @sebastian-c pointed out.

like image 192
Yihui Xie Avatar answered Nov 15 '22 20:11

Yihui Xie


So what you want is all the packages which are loaded except the base packages and knitr. If I then list all the packages and exclude those, you'll get what you want:

p <- setdiff(.packages(), 
        c("knitr", "stats", "graphics", "grDevices", "utils", "datasets", 
          "methods", "base"))
p

You'll have to make some exceptions say if you're making a knitr document about making things in knitr or if you want to explicitly load base packages.

like image 39
sebastian-c Avatar answered Nov 15 '22 22:11

sebastian-c


The problem with this approach is that the \Sexpr{} within the \AtEndDocument{} block in the preamble is evaluated at knit-time (the beginning of the .Rnw file, so it returns an empty list. In the generated .tex file, this appears as

\AtEndDocument{
\medskip
\textbf{Packages used}: .
}

The only way this will work is to include the code to generate this text explicitly at the end of the .Rnw file (which in my case is a child documenht, e.g.,

...
\bibliography{graphics,statistics}

Inside child document:
\textbf{Packages used}: \Sexpr{setdiff(.packages(), 
        c("knitr", "stats", "graphics", "grDevices", "utils", "datasets", 
          "methods", "base"))}.
like image 38
user101089 Avatar answered Nov 15 '22 20:11

user101089