opts_knit$set(root.dir = ) working in knit but not chunks

This is a repeat of a question asked on stackoverflow (https://stackoverflow.com/questions/63250597/), the responder didn't have an answer and suggested I ask here.

I have a Rmd file saved in a folder one below my project([path]/scripts), and the data is in a parallel folder ([path]/data) and I can't seem to set the working directory and access the data when running the chunks. I use the setup chunk, as below, and then run code in a following chunk. If I knit the whole document it works fine but if I run the 2nd chunk on its own I get "Error: 'data/warriner_2013_emotional_valence.csv' does not exist in current working directory ('[path]/scripts')" .

If I create a chunk with the command getwd() I get [path]/scripts if I manually run the chunks. If I knit them, I get [path] - which is where I want it. This seems to corroborate it being a run vs knit issue.


    ```{r setup, include=FALSE}
    require("knitr")
    opts_chunk$set(echo = TRUE)
    opts_knit$set(root.dir = "..")
    ```

    ```{r}
    library(tidyverse)
    war <- read_csv('data/warriner_2013_emotional_valence.csv')
    ```

Other solutions I've found say to run the code in a separate chunk to the setup, which I'm doing.
I've also tried changing the R Markdown Global Options "Evaluate chunks in directory: 'Project'" - which has no effect.

    >sessionInfo()
    R version 4.0.2 (2020-06-22)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 18362)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=English_United Kingdom.1252 
    [2] LC_CTYPE=English_United Kingdom.1252   
    [3] LC_MONETARY=English_United Kingdom.1252
    [4] LC_NUMERIC=C                           
    [5] LC_TIME=English_United Kingdom.1252    
    
    attached base packages:
    [1] stats     graphics  grDevices utils    
    [5] datasets  methods   base     
    
    other attached packages:
     [1] broom_0.7.0     forcats_0.5.0  
     [3] stringr_1.4.0   dplyr_1.0.1    
     [5] purrr_0.3.4     readr_1.3.1    
     [7] tidyr_1.1.1     tibble_3.0.3   
     [9] ggplot2_3.3.2   tidyverse_1.3.0
    [11] knitr_1.29     
    
    loaded via a namespace (and not attached):
     [1] Rcpp_1.0.5       cellranger_1.1.0
     [3] pillar_1.4.6     compiler_4.0.2  
     [5] dbplyr_1.4.4     tools_4.0.2     
     [7] digest_0.6.25    lubridate_1.7.9 
     [9] jsonlite_1.7.0   evaluate_0.14   
    [11] lifecycle_0.2.0  gtable_0.3.0    
    [13] pkgconfig_2.0.3  rlang_0.4.7     
    [15] reprex_0.3.0     cli_2.0.2       
    [17] DBI_1.1.0        rstudioapi_0.11 
    [19] yaml_2.2.1       haven_2.3.1     
    [21] xfun_0.16        withr_2.2.0     
    [23] xml2_1.3.2       httr_1.4.2      
    [25] fs_1.5.0         generics_0.0.2  
    [27] vctrs_0.3.2      hms_0.5.3       
    [29] grid_4.0.2       tidyselect_1.1.0
    [31] glue_1.4.1       R6_2.4.1        
    [33] fansi_0.4.1      readxl_1.3.1    
    [35] rmarkdown_2.3    modelr_0.1.8    
    [37] blob_1.2.1       magrittr_1.5    
    [39] backports_1.1.7  scales_1.1.1    
    [41] ellipsis_0.3.1   htmltools_0.5.0 
    [43] rvest_0.3.6      assertthat_0.2.1
    [45] colorspace_1.4-1 stringi_1.4.6   
    [47] munsell_0.5.0    crayon_1.3.4

I think one the best solution around here is to use non-relative pass, and get helps for that from this package

You could write

war <- read_csv(here::here('data/warriner_2013_emotional_valence.csv'))

and that will generate a full path to your file base on the root of your project. This way the R chunk will always managed to read your csv file no matter where the Rmd file is.

If knitting goes well, it means the option is working. If you run the chunk with csv reading on its own, you need to be sure the setup chunk is run before. In RStudio IDE, the green arrow to run chunk should insure that. Are you using RStudio ?

Just checked and it is not working... I remember it was... strange :thinking:
But it is possible that knitr option only works when rendering and you'll need to change Rstudio settings as described here:

Just checked and it is not working... I remember it was... strange :thinking:
But it is possible that knitr option only works when rendering and you'll need to change Rstudio settings as described here:

and I double check using an old RStudio version 1.2 instead of 1.3 and it works as expected in 1.2 !
If you set root.dir in the setup chunk, it will be taken into account in the other chunks.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.