When I process Rmd markup files that used cached fragments in RStudio using the Knit HTML button, I found that the order in which packages were loaded was not remembered from piece to piece. This causes problems when I need to download packages in a specific order to avoid namespace conflicts.
For a reproducible example (for which you need to install the plyr, dplyr, and pryr packages, see below), I start by creating a knr Rmd document that loads plyr and then dplyr (which both exports the summarise function), then uses pryr to determine what summation function is found. I knit this using the RStudio "Knit HTML" button:
```{r} library(knitr) opts_chunk$set(cache = TRUE, message = FALSE) ``` ```{r test1} library(plyr) library(dplyr) ``` ```{r test2, dependson = "test1"} attr(pryr::where("summarise"), "name") ```
As recommended here , I load plyr before dplyr so that dplyr functions come first in the search path. As expected, the md output file shows that the summarise function comes from dplyr:
attr(pryr::where("summarise"), "name")
However, if I make a small change in the test2 block:
```{r test2, dependson = "test1"} attr(pryr::where("summarise"), "name")
which forces it to recompile, now it loads the packages in the wrong order, and summarise is in plyr:
attr(pryr::where("summarise"), "name")
Please note that this problem does not occur if you run knit from the R command line, but this is only due to the fact that it stores the plyr and dplyr packages loaded into the environment (if I restart R, the same problem arises).
I know that I can reference functions like dplyr::summarise to avoid redundancy, but this is rather cumbersome. Not to download plyr is not an option at all, as several packages inadvertently add it to the namespace. How can I ensure that packages are downloaded in the correct order?
I am using the latest version of RStudio (0.98.1079) and my sessionInfo is below:
#
Note that if necessary, you can configure the necessary packages for this reproducible example:
```{r} install.packages(c("devtools", "plyr", "dplyr")) devtools::install_github("hadley/pryr") ```