I have a project folder, which is my working directory. Let's call it project. Under the project folder are 4 subdirectories:
code
data
figures
documents
.Rmd
code
figures
.html
.md
.docx
documents
data
project
.Rmd
```{r setglobal, cache = FALSE, include = TRUE}
library(knitr)
opts_knit$set(root.dir = "..")
```
```{r setchunk, cache=FALSE, include=TRUE}
opts_chunk$set(fig.path = "./figures/")
getwd()
```
project
data
```{r readdata}
crctx <- readRDS("./data/crctx.rds")
getwd()
*run lots of analyses here*
```
project/code/figures
project/figures
base.dir
base.dir = "./figures"
opts_knit$set
opts_chunk$set(fig.path = "./figures/")
opts_knit$set(root.dir = normalizePath("../"))
"./data"
opts_chunk$set(fig.path = "../figures/")
../
./
project/documents
project/code/figure
projects/documents
knit("./code/knitr_file.Rmd", "./documents/knitr_output.md”)
pandoc("./documents/knitr_output.md", format = "docx”)
pandoc('knitr_output.md', format='html') # HTML
pandoc('knitr_output.md', format='latex') # LaTeX/PDF
pandoc('knitr_output.md', format='docx') # MS Word
pandoc('knitr_output.md', format='odt') # OpenDocument
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}
library(knitr)
opts_knit$set(root.dir=normalizePath('../'))
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
plot(dat)
# dev.off()
```
```{r move}
files <- list.files("code/")
index <- grep("html", files)
file.rename(file.path("code", files[index]),
file.path("documents", files[index]))
```