简体   繁体   中英

R directory path windows vs Mac

I have a number of R Projects which run on my Windows machine and I recently bought a Mac.

I would like to alternate between my Mac and Windows pc without changing code snippets all the time.

I use variables that serve as directory/path locations.

For example:

temp_path <- 'C:/RCode/'

If I run this code on my Mac, I will obviously get an error.

How can I get around this?

You can specify path based on OS using an if condition. To find the OS you can use *.Platform$OS.type. Here is a sample code to make it work.

OS <- .Platform$OS.type

if (OS == "unix"){
  temp_path <- "~/RCode/" # MAC file path
} else if (OS == "windows"){
  temp_path <- "C:/RCode/" # windows file path
} else {
  print("ERROR: OS could not be identified")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM