Problems setting root.dir in knitr

RStudio sets the working directory to the project directory, but knitr sets the working directory to the .Rmd file directory. This creates issues when you are sourcing files relative to the project directory in your R markdown file. Specifically, knitr tells you it can’t find those files:

Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'home/sus/Documents/research_phd/analysis/phenodata_explore.R': No such file or directory

knitr has an option for dealing with this – root.dir – and Phil Mike Jones even gives a nice little knitr chunk example of using it.

But when I tried it, knitr kept failing to find my file.

```{r "setup", include=FALSE}
require("knitr")
opts_knit$set(root.dir = "~/Documents/research_phd/")
source('analysis/someanalysis.R')
```

It turns out I should have read the documentation more closely.

Knitr’s settings must be set in a chunk before any chunks which rely on those settings to be active. It is recommended to create a knit configuration chunk as the first chunk in a script with cache = FALSE and include = FALSE options set. This chunk must not contain any commands which expect the settings in the configuration chunk to be in effect at the time of execution.

Breaking it into two chunks separating the knitr configuration from the sourcing solved the problem.

```{r "knitr config", cache = FALSE, include=FALSE}
require("knitr")
opts_knit$set(root.dir = "~/Documents/research_phd/")
```

```{r, "setup", echo = FALSE}
source('analysis/someanalysis.R')
```
Tagged

Natural history, synthesis papers and the academic caste system | Small Pond Science

Everybody loves natural history. Everybody thinks that we should value natural history more (well, almost everybody), and find a way to value descriptive field biology more in our academic rewards system. I would bet there are more ecologists who hate puppies than ecologists who think that natural history is valued adequately.

Though nearly all of us agree that natural history need to be valued more, if you look at the way we publish and cite and hire and tenure and promote and award scientists, well, we pretty much aren’t valuing it.

We want it to be valued because we all benefit from it. But we all are reluctant to do and publish much of it because we aren’t getting credit for it.

Source: Natural history, synthesis papers and the academic caste system | Small Pond Science

Tagged ,

While genecological studies suggest divergent selection on phenotypic traits for local adaptation to climate must be relatively strong (Howe et al. 2004; Savolainen et al. 2007; Alberto et al. 2013), population genetic studies suggest gene flow is high, as most widespread species show weak- to-moderate population differentiation (FST) for selectively neutral genetic markers (Kremer et al. 2012). How tree populations could diverge substantially for locally adaptive traits in the face of high levels of gene flow has been something of a puzzle (Savolainen et al. 2007). Theoretical modelling suggests that highly polygenic traits controlled by many co-varying loci of small effect can create phenotypic divergence under divergent selection despite high gene flow, but individual loci underlying such traits will have only weak divergence, and will be difficult to detect (Latta 2003; Le Corre and Kremer 2012; Savolainen et al. 2013). This genetic architecture presents a challenge for popula- tion studies to detect and adequately characterize local adaptation through genome scans

Aitken, S. N., & Bemmels, J. B. (2015). Time to get moving: Assisted gene flow of forest trees. Evolutionary Applications, doi:10.1111/eva.12293

Maybe I can solve a little piece of this puzzle with my PhD….

How fast did Pinaceae ranges move in the past? | The Modern Forest

Source: How fast did Pinaceae ranges move in the past? | The Modern Forest

I remade a couple graphs from Ordonez & Williams 2013 to get a better look at the Pinaceae.