Reputation: 2240
I use R to work with my google drive on 2 windows machines as well as my macbookpro. Depending on which machine I am on, I end up un-commenting and commenting out the lines of code that start a script as shown here:
#setwd("C:/Users/sweetusername/Google Drive/projectX") # win10 work
#setwd("~/Google Drive/projectX") # macbookpro
setwd("C:/Users/sweetusername_butslightlydifferent/Google Drive/projectX") # win10 home
I'd like to be able to check as such:
ifelse(operating system == mac, setwd("~/Google Drive/projectX"),
ifelse(find C:/Users/sweetusername,
setwd("C:/Users/sweetusername/Google Drive/projectX"),
ifelse(find C:/Users/sweetusername_butslightlydifferent,
setwd("C:/Users/sweetusername_butslightlydifferent/Google Drive/projectX"),
print("Location Not Found: Check Directory Structure"))))
I know this is pretty cludgy thinking, and someone has elegantly thought of how to do this. I have found where people scan a directory for files, so i can get the syntax for the last two parts. But checking if OS is mac or even linux would be very useful for me and I've not found how to do that.
I am aware that using a project
in rstudio would be ideal, but I have issues with Google Drive trying to sync that file and locking it every single time any change is made to it. It is very irritating, so I have not been using Rstudio projects.
Upvotes: 0
Views: 557
Reputation: 9656
The data about your machine should be available in Sys.info()
.
Mac OS will typically have a "Darwin" as it's name so you could use it to check if you are on a mac machine:
sysname <- Sys.info()["sysname"]
if(sysname == "Darwin") {
setwd("~/Google Drive/projectX") # example on mac machine
} else if(sysname == "Linux") {
setwd("~/GoogleDrive/projextX") # example on linux machine
} else if(sysname == "Windows") {
setwd("C:/Users/sweetusername/Google Drive/projectX") # example on win machine
}
Thanks to @RLave for providing the sysname value for Windows.
Alternative way is to only check based on file-paths and set the working directory to the first available directory in your list:
locations <- c("~/Google Drive/projectX",
"C:/Users/sweetusername/Google Drive/projectX",
"C:/Users/sweetusername_butslightlydifferent/Google Drive/projectX",
)
setwd(Find(dir.exists, locations))
Paths starting with tilde ~
will not be available on Windows machines - so this will also differentiate between macos and windows.
Upvotes: 2