2

I use this code to set file location as path/working directory automatically:

```{r,echo=FALSE, warning=FALSE, message=FALSE}
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
```

However, sometimes when I pres Ctrl+Alt+R to run all chunks, there is an error and when I run this specific chunk (above) it starts working. This happens in a rare cases but its problem for me. Do you see any reason/solution?

Error in setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) : 
  cannot change working directory

Error in setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) : 
cannot change working directory
20.
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
19.
eval(expr, envir, enclos)
18.
eval(expr, envir, enclos)
17.
withVisible(eval(expr, envir, enclos))
16.
withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, 
error = eHandler, message = mHandler)
15.
handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, 
enclos)), warning = wHandler, error = eHandler, message = mHandler))
14.
timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, 
envir, enclos)), warning = wHandler, error = eHandler, message = mHandler)))
13.
evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, 
debug = debug, last = i == length(out), use_try = stop_on_error != 
2L, keep_warning = keep_warning, keep_message = keep_message, 
output_handler = output_handler, include_timing = include_timing)
12.
evaluate::evaluate(...)
11.
evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning), 
keep_message = !isFALSE(options$message), stop_on_error = if (is.numeric(options$error)) options$error else {
if (options$error && options$include) 
0L ...
10.
in_dir(input_dir(), evaluate(code, envir = env, new_device = FALSE, 
keep_warning = !isFALSE(options$warning), keep_message = !isFALSE(options$message), 
stop_on_error = if (is.numeric(options$error)) options$error else {
if (options$error && options$include) ...
9.
eng_r(options)
8.
block_exec(params)
7.
call_block(x)
6.
process_group.block(group)
5.
process_group(group)
4.
withCallingHandlers(if (tangle) process_tangle(group) else process_group(group), 
error = function(e) {
setwd(wd)
cat(res, sep = "\n", file = output %n% "") ...
3.
process_file(text, output)
2.
knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
1.
rmarkdown::render("TPR.rmd", output_file = "HTML_doc")
  • Is there any pattern to when this happens? i.e. does it happen when opening notebooks from network drives or flash drives etc? – JaredS Jun 19 '22 at 11:14
  • I always open it from the same folder which locates on OneDrive (cloud) –  Jun 19 '22 at 11:22
  • It could be that if the `rstudioapi::getActiveDocumentContext()` command is being run while the notebook file is being synced it may be unable to determine the location of the file. I don't know the details of OneDrive's sync mechanism so I can't be certain. I'd recommend trying this with the notebook saved in a local folder and see if it still happens. – JaredS Jun 19 '22 at 12:01
  • It still happens –  Jun 19 '22 at 13:27
  • As a general rule, I believe that setting the working directory in code is not good practice. (Not just for reasons like this!) COde should leave no footprints. By setting the working directory you leave a side effect that all future code in the session may need to deal with. Unless you restore the previous working directory when the code has finished executing. Which is easy to forget to do. Far better, I believe, to use paths - either full or relative depending on context - to access resources outside the current working directrory. – Limey Jun 19 '22 at 14:38
  • automatizing path setting through code is crucial for me at that moment –  Jun 19 '22 at 14:53
  • 2
    It's your call, of course, but I would strongly suggest rethinking your workflow. See [here](https://stackoverflow.com/questions/13770304/risks-of-using-setwd-in-a-script) for a discussion. And, if you are responding to a specific comment from a user, please @ them in your response. Otherwise, the only way they know about your response if they take the trouble to revisit your question. – Limey Jun 19 '22 at 15:06

0 Answers0