Reproducibility problems with Rmarkdown and Renv

旧街凉风 提交于 2020-06-13 08:05:53

问题


I recently noticed that the same R markdown document was generating different plots when being run in different conditions (eg. different projects). As a toy example, consider the following document:

---
title: "Example"
output:
  html_document
---

```{r plot, warning=FALSE, echo=FALSE}
library("tidyverse")
tibble(mu = c(-9.4, -9.3, -9.2, -9, -8.9, -8.8, -8.7, -8.5, -8.4, -8.3, 0),
       N = c(1, 2, 1, 1, 1, 3, 1, 1, 1, 1, 3)) %>%
  ggplot() +
  geom_point(aes(x=mu, y=N)) + 
  geom_bar(aes(x=mu, y=N), stat="identity", fill="grey", colour="black", width=0.1) +
  theme(panel.background=element_blank(),
        axis.title=element_text(size=20),
        axis.text=element_text(size=15))
```

It should produce a plot like the following one, and sometimes I am successful and get it:

However, other times, I get this alternate version, which is nonsense:

I have noticed that after loading the tidyverse package, some versions are different. For example, in the first one ggplot is 3.2.1, while in the later it is 3.3.0. This later has a different version number because it is maintained separately in a R project using Renv.

Ok, could it be the different version numbers? Even given these different versions as I known this package from long ago it is difficult to me to understand how the second plot could have been generated given this code in any ggplot version. Any hints on what could be causing this problem, if not the version numbers?


回答1:


The problem is that it sometimes guesses one orientation, sometimes the other. (Your bad plot has horizontal bars.) You can force vertical bars using orientation = "x" in the geom_bar call.

I'd call it a bug that it seems to choose orientation non-deterministically (but see comment below). I saw the same thing on my system. I only used ggplot2 version 3.3.0; the only difference was the history of what code I'd run before.

EDITED TO ADD: After a lot more testing, I can't reproduce the non-reproducibility :-). ggplot2 has some complicated rules for guessing at orientation; they are unstable with respect to tiny changes to the data, but seem stable with fixed data. I'm guessing I accidentally changed the character of the dataset without realizing it.



来源:https://stackoverflow.com/questions/61235860/reproducibility-problems-with-rmarkdown-and-renv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!