【问题标题】:Can R know if it is running on Windows or not?R 可以知道它是否在 Windows 上运行吗?
【发布时间】:2023-11-19 06:09:01
【问题描述】:

我想知道 R 中是否有一个逻辑测试来评估 R 是否在“Windows”(win) 操作系统下运行?

这是做什么用的:

if("OS is windows") { ## Here is the logical test I need !!!

  setwd("~/../Desktop")  

  } else {

  setwd("~") }          


x <- paste0(getwd(),"/", "Animation")
dir.create(x)

【问题讨论】:

    标签: r macos operating-system


    【解决方案1】:

    这是一个可能有帮助的函数:

    https://github.com/hadley/rappdirs/blob/master/R/utils.r#L1

        get_os <- function() {
      if (.Platform$OS.type == "windows") { 
        "win"
      } else if (Sys.info()["sysname"] == "Darwin") {
        "mac" 
      } else if (.Platform$OS.type == "unix") { 
        "unix"
      } else {
        stop("Unknown OS")
      }
    }
    

    【讨论】: