R: determine if a script is running in Windows or Linux

前端 未结 4 1369
星月不相逢
星月不相逢 2020-12-08 13:39

Is there a simple way to programmatically determine if an R script is being executed in Windows vs. Linux?

相关标签:
4条回答
  • 2020-12-08 14:02
    .Platform$OS.type
    

    returns

    [1] "unix"
    

    or something else.

    0 讨论(0)
  • 2020-12-08 14:09

    I run the same code from any of three Linux or Windows machines. I use the following to set up working directories:

    if(R.Version()$os == "linux-gnu"){
      dir.pre <- "/home"
    } else {
      dir.pre <- "C:/Users"
    }
    

    On my debian linux server and my Ubuntu laptop:

    > .Platform$OS.type
    [1] "unix"
    > R.Version()$os
    [1] "linux-gnu"
    

    On my Windows 10 laptop, in RStudio:

    > .Platform$OS.type
    [1] "windows"
    > R.Version()$os
    [1] "mingw32"
    

    Feel free to edit and add to this list.

    0 讨论(0)
  • 2020-12-08 14:10
    Sys.info()["sysname"]
    
    0 讨论(0)
  • 2020-12-08 14:20
    if(.Platform$OS.type == "unix") {
    } else {
    
    }
    
    0 讨论(0)
提交回复
热议问题