You could store the root directory of the project at the top of the script using the function find_rstudio_root_file()
in package rprojroot:
library(rprojroot)
root.dir = find_rstudio_root_file()
setwd("other_dir")
# some code working in other dir
setwd(root.dir)
# come back to the root of the project