SilvaC
SilvaC

Reputation: 141

Setting Working Directory using knitr::opts_knit

I am trying to use the code below to change the working directory in R markdown (using the R setup chunk). However, this doesn't seem to be working. The output from getwd() after running this chunk remains as the directory containing the R script. I've never had this issue before; any ideas of where I'm going wrong? Thank you!

```{r setup}
library(foreach)
library(stringr)
library(rmarkdown)
library(knitr)

knitr::opts_knit$set(root.dir = normalizePath('/Users/chantal/Documents/PhD/Projects/'))
getwd()
```

Upvotes: 2

Views: 622

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

Based on this answer I believe that if you check getwd() in the next chunk, you should see that the working directory has changed. This is something I tried and it appears to corroborate Tomas' comment.

---
title: "test"
author: "Roman Luštrik"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=TRUE}
knitr::opts_knit$set(echo = TRUE, root.dir = normalizePath("/home/romunov/Downloads"))
getwd()
```

```{r}
getwd()
```

enter image description here

Upvotes: 1

Related Questions