Working with knitr using subdirectories

后端 未结 2 1255
逝去的感伤
逝去的感伤 2020-12-28 16:31

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

2条回答
  •  粉色の甜心
    2020-12-28 16:52

    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]))
    ```
    

提交回复
热议问题