0

So I am using RMarkdown and I am sourcing code from a script contained in a separate folder (Scripts) at the same level as the folder which contains the Markdown file (Report). Both directories are at the root of the R project.

I achieve that first by setting the working directly using the following code in the setup chunk:

knitr::opts_knit$set(root.dir = normalizePath(".."))

Then calling the following in a R chunk:

```{r datacreation, echo = FALSE}
source("./Scripts/20212022Summary.R")
```

All that is great but I also want to display later on in the knitted document, the whole code contained in that script so it can be reviewed if necessary.

To achieve this, I do :

```{r 20212022Summary_readlinesversion, code = readLines("../Scripts/20212022Summary.R"),echo=TRUE,include = knitr::is_html_output(),cache=FALSE,eval = FALSE}```

Which just shows the code without running it, with a nice formatting.

What I would like to know is how I would go about defining once and for all what is my working directory for consistency sake. Specifically when I call code = readLines("../Scripts/20212022Summary.R") I clearly operate with the knowledge it will consider the working directory to be the one containing the report instead of the one (parent directly aka project root directory) which I defined previously in the set up chunk.

Ideally I would define things once and for all, especially as then I could pick those up programmatically and source from anywhere within the project folder, consistently.

Anyone has tips on how to achieve this? What I have done works, it's just ugly, manual and not consistent. And it bugs me :)

cuisquare
  • 30
  • 9
  • 1
    Changing the working directory is generally considered to be [poor practice](https://stackoverflow.com/questions/13770304/risks-of-using-setwd-in-a-script). Better to *get* the working directory and then construct relative file paths if the location of the inputs. – Limey Aug 25 '22 at 14:49
  • The `here` package is a nicely developed and maintained solution to this problem. With `library(here)` you would use `source(here("Scripts/20212022Summary.R"))` – Gregor Thomas Aug 25 '22 at 14:59
  • I'm not sure I understand what is meant by changing the working directory is poor practice. Typically in a project it's by default the root directory. And when knitting a document by default it's the directory the document lives in. I want to be able to source files and have all relative path in all possible calls in that file, be the same whether I am knitting or scripting within that project. It stands to reason that I then would want a single consistent working directory. – cuisquare Aug 25 '22 at 19:49
  • thanks for mentioning the here package. It looks like it will give more freedom as per where my files are. But I am unsure it will solve the "main" issue which is that depending whether I am running r code chunks or colling the code option in the header of a code chunk effectively the working directory is not the same. In the first case it is whatever I set it to (directory containing the markdown file otherwise) in the the second case it is the default without seemingly anyway to change it. I will give a go at inserting a here call in readLines and see if that works, fingers crossed! – cuisquare Aug 25 '22 at 19:57

0 Answers0