Bri*_*son 90
switch(Sys.info()[['sysname']],
Windows= {print("I'm a Windows PC.")},
Linux = {print("I'm a penguin.")},
Darwin = {print("I'm a Mac.")})
Run Code Online (Sandbox Code Playgroud)
由于我花了很多时间来解决这个问题,我认为其他人也会受益.
问候,
Jos*_*ich 32
我不确定使用,Sys.info()因为帮助页面说它没有在所有R平台上实现; 也许用.Platform呢? ?.Platform有很多有用的信息,因为:
'.Platform'是一个列表,其中包含构建R的平台的一些细节.这提供了编写OS便携式R代码的方法.
它似乎也包含在R上的包使用.Platform 多比更频繁Sys.info.
josh: /c/R/R-2.12.0-src/src/library
> grep ".Platform" */R/* | wc -l
144
josh: /c/R/R-2.12.0-src/src/library
> grep ".Platform\$OS.type" */R/* | wc -l
99
josh: /c/R/R-2.12.0-src/src/library
> grep "Sys.info" */R/* | wc -l
4
Run Code Online (Sandbox Code Playgroud)
> Sys.info()
sysname
"Linux"
release
"2.6.32-26-generic"
version
"#48-Ubuntu SMP Wed Nov 24 09:00:03 UTC 2010"
Run Code Online (Sandbox Code Playgroud)
小智 5
由于Sys.info()和.Platform$OS.type会根据运行的操作系统产生不同的结果,因此我进行了更多搜索,并在https://www.r-bloggers.com/identifying-the-os-from-r/上找到了以下函数
get_os <- function(){
sysinf <- Sys.info()
if (!is.null(sysinf)){
os <- sysinf['sysname']
if (os == 'Darwin')
os <- "osx"
} else { ## mystery machine
os <- .Platform$OS.type
if (grepl("^darwin", R.version$os))
os <- "osx"
if (grepl("linux-gnu", R.version$os))
os <- "linux"
}
tolower(os)
}
Run Code Online (Sandbox Code Playgroud)