Mark Danese Mark Danese - 1 year ago 61
R Question

Working with knitr using subdirectories

I have a project folder, which is my working directory. Let's call it project. Under the project folder are 4 subdirectories:

, and

I would like to put my
file in the
subdirectory. I would like the figures to go into the
subdirectory. I would like the
, and
file(s) to go into the
subdirectory. I would like to read in my data from the
subdirectory. Is this organizational structure possible? I can't seem to get it to work.

I start with this to set the working directory to
because I understand that knitr looks to the folder with the
file and treats it as the working directory if this is not specified.

```{r setglobal, cache = FALSE, include = TRUE}
opts_knit$set(root.dir = "..")

Then I try setting the figure path relative to the working directory.

```{r setchunk, cache=FALSE, include=TRUE}
opts_chunk$set(fig.path = "./figures/")

The working directory is correctly reported as
. The data is correctly read from the
subdirectory. All output is rendered properly and all code runs.

```{r readdata}
crctx <- readRDS("./data/crctx.rds")
*run lots of analyses here*

But the figures end up in the
directory instead of

I have also tried setting
but that doesn't seem to change anything. I added the parameter

base.dir = "./figures"

to the
list. I even tried hard-coding the full path. But nothing seems to be able to change this.

I am using R 3.10 and RStudio 0.98.953. This is Mac 0SX 10.9.4.

Am I missing something? And is there a way to put the output files in their own directory?

The reason this is important is that I would like our company to all use the same directory structure, and this will allow us to organize our projects better.

Thanks in advance for any help.


I realize that
opts_chunk$set(fig.path = "./figures/")
is incorrect. I assumed that setting the root directory to the project folder using
opts_knit$set(root.dir = normalizePath("../"))
would make a global change to knitr, since that is what it is supposed to do. And it does work for the data subdirectory, which can now be accessed with
which allows the code to run. However, the global setting doesn't apply to the figure output. Therefore, the correct specification is
opts_chunk$set(fig.path = "../figures/")
-- using
instead of
. I think Richie Cotton fixed this, and I didn't quite see it for a while.

I also used Terminal in OSX to create a symbolic link from
which is created by default by knitr. With this, knitr looks to the right sub-subdirectory, but everything ends up in
. That worked really well. I can't get R do create the symbolic link properly using file.symlink. But it works fine in Terminal. Go figure.

update 2:

I got the output files to work as well. You have to use the knit command directly. The text is below.

Knitr code to knit a markdown document with .Rmd in code/analysis and output in output/reports:
knit("./code/knitr_file.Rmd", "./documents/”)

Pandoc code to convert .md file to .docx -- uses the same folder as the .md file
pandoc("./documents/", format = "docx”)

output styles (from knitr documentation)

pandoc('', format='html') # HTML
pandoc('', format='latex') # LaTeX/PDF
pandoc('', format='docx') # MS Word
pandoc('', format='odt') # OpenDocument

Answer Source

Try this. It assumes you have the 4 folders you listed inside the working directory project. It also assumes you have a .csv file called myData.csv in data.

When you knit the file, the plot will be saved in figures. At the end, the code looks for html files in code and moves them to documents. There's probably a better way to do this.

```{r setup}
  opts_chunk$set(fig.path = "../figures/", dev='pdf') # corrected path and added dev

```{r import}
  dat <- read.csv("data/myData.csv")

```{r plot}
  # pdf(file="figures/test.pdf")  # I do this in setup instead

```{r move}
  files <- list.files("code/")
  index <- grep("html", files)
  file.rename(file.path("code", files[index]),
              file.path("documents", files[index]))