knitr opts_knit$set(root.dir = "../") ignored when chunk_output_type: console

My actual working directory of a .Rmd file is "C:/Users/Camilo Erasso/Documents". I want to change it using knitr::opts_knit$set(root.dir ="D:/CAMILO") in the setup chunk.

This works fine whit the default YAML option: editor_options: chunk_output_type: inline. But when I change this option to editor_options: chunk_output_type: console the new working directory is ignored or not changed.

I'm not used to work whit inline result (I prefer the console), for that reason I use this option. The same option can be changed in RStudio>Tools>Global Options...>R Markdown>Show output inline for all R Markdown documents (uncheck) or in the .Rmd setting buttom (next to Knit buttom)> Chunk Output in Console

Toy example:

---
title: "Example root.dir change"
author: "Camilo"
date: "2 de abril de 2019"
output: html_document
---

```{r setup, include=FALSE}
getwd() #[1] "C:/Users/Camilo Erasso/Documents"
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir ="D:/CAMILO")
```

```{r}
getwd() #[1] "D:/CAMILO"
```

But when using the console output option:

---
title: "Example root.dir change"
author: "Camilo"
date: "2 de abril de 2019"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
getwd() #[1] "C:/Users/Camilo Erasso/Documents"
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir ="D:/CAMILO")
```

```{r}
getwd() #[1] "C:/Users/Camilo Erasso/Documents" or NOT CHANGED
```

This problem is slightly different from this: https://github.com/yihui/knitr/issues/1575

And related whit this: https://github.com/rstudio/rmarkdown/issues/1077
and this https://stackoverflow.com/questions/41911487/html-notebook-ignores-global-chunk-options.

Is this and RStudio issue? Thanks for your help!!!

1 Like

It seems that the feature with a setup chunk that allows the IDE to know how to run chunks only works with inline mode. The default for notebook mode too.

If you knit your exemple, it is working correctly. The second chunk takes into acount you global option. The unwanted behavior comes from the IDE only.

I think it may be on purpose because console mode means "previous working mode for Rmd". The R code from chunk are sent to the console and executed. One chunk does not know about the other in this mode. Unless the document is knit and then it works as expected.

inline mode is the new mode where chunk are executed in a special context from the IDE. This special context know how to handle the setup chunk, and even allow to run all previous chunk if needed. It is this mode that offers the setup chunk feature to set knitr option for interactive use.

See this answer

Hope it helps

1 Like

You are totally right:

The unexpected come with this:

I noticed that if I use the traditional setwd("D:/CAMILO/UNIANDES/TESIS") in only one chunk, the first one or where I need it, with the console mode, it remains so for all the document (all chunks), als a .R script. This is obviosly not recomended when knit (if you don't put opts_knit$set(root.dir:.) in setup), but is a nice (but contradictory?) workaround.

The good thing is that this workaround doesn't have any problem when knit. I suspect that setwd() only rewrites the same (changed) directory in that chunk and the others are simply modified by opts_knit$set(root.dir:.).

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.