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")
7
  • Is there any pattern to when this happens? i.e. does it happen when opening notebooks from network drives or flash drives etc? Commented Jun 19, 2022 at 11:14
  • I always open it from the same folder which locates on OneDrive (cloud) Commented Jun 19, 2022 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. Commented Jun 19, 2022 at 12:01
  • It still happens Commented Jun 19, 2022 at 13:27
  • 2
    It's your call, of course, but I would strongly suggest rethinking your workflow. See here 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. Commented Jun 19, 2022 at 15:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.