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

后端 未结 5 1071
北荒
北荒 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:50

    Where you have an R project with nested subfolders, so that the .Rproj and .Rmd files are located in different folders, you can use the command rprojroot::find_rstudio_root_file() to find and set the working directory to the Project's main folder during Kniting (instead of the folder containing the rMarkdown code file).

    So at a minimum use the following:

    ```{r setup}
    
    knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
    
    ```
    

    inside the setup chunk.

    See also Automatically finding the path of current R project in R Studio and https://support.rstudio.com/hc/en-us/community/posts/220826588-Working-directory-in-R-Notebooks.

提交回复
热议问题