Description
I use the menu option "Session > Set Working Directory > To Source File Location" a ton and have started using this programmatically via
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
or
setwd(dirname(.rs.api.getSourceEditorContext()$path))
I'm curious if there could be a shorter command that mimics the Session menu option:
rstudioapi::setwdToSoureFileLocation()
or
setwd(rstudioapi::getSoureFileLocation())
The top two are fine, they are just verbose and I often have to look it up... "right, sourceEditor
not activeDocument
... oh yeah, add $path
... oh right, I need dirname()
"
I know it's been said not to use setwd()
in your code but we use github a ton and I don't always want to create a .Rproj
for a simple .R
+ .csv
combo. Anyone (mostly me) who has cloned a repo and opened a file should be able to set their working directory to that file's location.