Setting the working directory - Mac and PC


I have to use Macs and PCs.

It’s always been a mystery to me why I can’t use the same format to identify directory paths between the Mac and the PC.

So I always have this snippet of code in my scripts…

    # if Windows
    working_directory <- "C:\\Users\\Kurt\\Dropbox\\<work directory>”
    # if Apple
    if (['sysname'] == "Darwin")
        working_directory <- "~/Dropbox/<work directory>"

I realize that this is just a couple lines of code, but is there a way to simplify / streamline this issue? Thanks for any assistance


You could consider using here:


I would recommend using RStudio Projects. This will result in the project folder always being set as the working directory and then you can simply use relative paths. I work on both a mac and PC and projects prevent this issue, especially when used with git/GitHub