Setting work directory in knitr using opts_chunk$set(root.dir = …) doesn't work

后端 未结 5 1073
北荒
北荒 2020-12-05 17:39

My R project is structured like a package with directories /R, /vignettes, /data etc. In one of my Rmd docs in /vignettes

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 17:51

    Some details on implementation of setting work directory by root.dir =.
    Although there already are some awesome answers by Yihui and Tommy. I still got stuck in setting work directory. So I am trying to make a complete answer here.

    1. From knitr documentation:

    Knitr’s settings must be set in a chunk before any chunks which rely on those settings to be active. It is recommended to create a knit configuration chunk as the first chunk in a script with cache = FALSE and include = FALSE options set. This chunk must not contain any commands which expect the settings in the configuration chunk to be in effect at the time of execution.

    1. My code example:

    In my case, the .Rproj and .Rmd files are located in the same folder.

    ```{r setup, include=FALSE, cache = FALSE}
    require("knitr")
    ## setting working directory
    opts_knit$set(root.dir = "~/Documents/R/Example")
    ```
    

提交回复
热议问题