R目录路径Windows vs Mac

时间:2019-01-22 12:07:00

标签: r windows macos portability

我有许多在Windows机器上运行的R项目,最近我购买了Mac。

我想在Mac和Windows PC之间切换,而不一直更改代码段。

我使用用作目录/路径位置的变量。

例如:

temp_path <- 'C:/RCode/'

如果我在Mac上运行此代码,显然会得到一个错误。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您可以使用if条件基于OS指定路径。要查找操作系统,可以使用* .Platform $ OS.type。 这是使其工作的示例代码。

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")
}