

Versions of macOS prior to 10.15 Catalina include a copy of GNU Emacs 22 without GUI support compiled in and thus Emacs is automatically available on all but the most recent versions of macOS via the terminal. You can find precompiled versions of emacs and Emacs.app at. NameOfMyFile <- sub(".The official Emacs fully supports Mac OS X (along with GNU/Linux, Windows, DOS, and then some). # returns the path including the name of the R file you're runningĬompletePath<- rstudioapi::getActiveDocumentContext()$path Applying regular expressions allows you to separate the path from the filename and you can set our working directory path. You can subset the path with the $-operator. If copy paste rstudioapi::getActiveDocumentContext() into the console, your context is your console and thus it will return an empty path. Note: It is important to run rstudioapi::getActiveDocumentContext() from your R Studio editor, because you want information about your R file. path: '~/Desktop/MyBlog/content/post/-changing-working-directories.Rmd' Among other things, this includes the path and the name of the R file you just ran. It returns a dataframe with information about your document context. Run rstudioapi::getActiveDocumentContext().

After reading and desperately trying things like this.dir <- dirname(ame(2)$ofile) But what if you changed the folder structure on your computer or your poor collegue tries to run your script after cloning your repo from git? I wanted to find a way to set the working directory in an R file automatically. This is error prone! Especially if you run your script on a regular basis for reports! And yes, you could copy paste your path. You can check your working directory with getwd() getwd()īut this only works for jobs you're starting manually and you have to remember setting the working directory. Session > Set Working Directory > To Source File Location R will print the code how to set your new working directory into the console setwd("~/Desktop/") Use the click interface of R Studio (I do not recommend that)
