Quick R tip- making scripts work across OS’s

Quick thought for newbies to R. I have spent four years loading in data like this

mydatafirst=read.csv("D:\Dropbox\R-files\Project1\etc.")

on my Windows machine and like this

mydatafirst=read.csv("/home/chris/Dropbox/R-files/Project1/etc.")

on my Linux machine. I would comment out the one I wasn’t using at the time.

It’s fine for a while but once you start reading multiple dataframes (particularly if they’re not all loaded at the start) it gets very fiddly and annoying. Don’t do this. Just set your working directory. You can do it manually:

setwd(“D:\Dropbox\R-files\etc.”)

Or, even better, the seamless option:

setwd(ifelse(Sys.info()['sysname']=="Windows";,
             "D:\Dropbox\R-files\Project1\",
             "~/Dropbox/R-files/Project1/"))

Mmm, I can almost taste the hours I’ll save in the course of a year.

Leave a Reply