[英]detecting operating system in R (e.g. for adaptive .Rprofile files)
我想知道如何在R中自动检测操作系统,例如将事物放置在.Rprofile中。
switch(Sys.info()[['sysname']],
Windows= {print("I'm a Windows PC.")},
Linux = {print("I'm a penguin.")},
Darwin = {print("I'm a Mac.")})
因为花了我不多的时间来解决这个问题,所以我认为其他人也会受益。
问候,
我不确定要使用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
> Sys.info()
sysname
"Linux"
release
"2.6.32-26-generic"
version
"#48-Ubuntu SMP Wed Nov 24 09:00:03 UTC 2010"
由于Sys.info()
和.Platform$OS.type
根据运行的操作系统产生不同的结果,因此我进行了更多搜索,然后在https://www.r-bloggers.com/identifying-the-os中找到了以下功能-从-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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.