1

I am using RStudio for development and installed last week the development version of RStudio (Version 1.0.34) since I wanted to play with R Notebooks. I now run into the problem, that the code chunks do not get executed related to the working directory but to the location of the markdown document. This was former common when knitr the document but not when executing the chunks.
I now downloaded the newest development version (1.0.44) but still the same problem. Is there a workaround to this problem or is this the future plan for RStudio and rMarkdown? I think this is very inconvenient when developing a document.

I am aware of this post In RStudio/RMarkdown, how to setwd? for example, but this is why I executed the chunks and not knit the whole document. It worked as expected in RStudio <= 1.0.0.

Community
  • 1
  • 1
drmariod
  • 11,106
  • 16
  • 64
  • 110

1 Answers1

1

It is the future plan; the idea is that the chunk should run identically whether run one at a time (interactively) or all at once in a batch (knitr). Using the same working directory for both of these execution modes gives a reliable starting point for relative paths inside the chunk.

If you don't like this behavior there are a couple of ways to opt out of it.

Set the directory in the setup chunk

The working directory for running chunks does not have to be the directory of the document; you can set it to whatever you want in the setup chunk.

```{r setup}
knitr::opts_knit$set(root.dir = ...)
````

Execute chunks in the console instead

If you prefer the behavior in the previous version of RStudio -- that is, executing chunks only sends the code to the console, where it is executed in the current working directory -- you can make it the default. Go to Tools -> Options -> R Markdown and uncheck Show output inline for all R Markdown documents.

enter image description here

Jonathan
  • 8,497
  • 41
  • 35
  • Thanks, this helps. But this means, if I uncheck the "Show output inline for all R Markdown documents" then this also means, I have the same disadvantages compared to the old markdown. Like the whole document needs to be compiled when I want to knit the Notebook? – drmariod Oct 20 '16 at 17:04
  • Is there an easy way to set the `root.dir` to the current working directory instead the location of the markdown file? This would actually solve the whole problem I guess :-) – drmariod Oct 20 '16 at 17:06
  • 1
    No, there's no way to do it ... yet. Enough people have asked for it however that we're likely going to add support. We still want to keep the symmetry between knitting + running interactively, however, so it'll be something you set in your document that causes chunks run in the current directory (and/or the project directory) both when running interactively and knitting. Would that do what you want? – Jonathan Oct 20 '16 at 18:24
  • I guess this is what I am looking for. I loved the functionality to use the RMD similar as a script which I can just run in my working directory without creating a copy of the RMD. But on the other hand, it is really nice to have figures etc inline together with the R code. So if you find a solution to get both function working, this would be awesome! :-) But for now I guess I will switch off the inline output to get everything working as expected. Thanks! – drmariod Oct 21 '16 at 05:32
  • 1
    @drmariod An old post but for reference you can use `opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())` to set the current working directory to the location of the `.Rproj` file. See https://stackoverflow.com/a/60159715/4241780. – JWilliman Feb 10 '20 at 23:06