To set the working directory to the source file location, you can do the following:
To get the source file directory:
dirname(rstudioapi::getActiveDocumentContext()$path)
To set the working directory to source file directory:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))