How to Get the Operating System in R Programmatically
R provides several built-in values and functions for identifying the operating system on which an R session is running. For a simple Windows-versus-Unix check, use the OS.type component of .Platform.
The .Platform object is a list containing platform-specific information associated with the current R installation, including the operating-system family, file separator, path separator, and executable-file extension.
To access the operating-system type, use .Platform$OS.type.
The syntax to access OS.type of .Platform is:
.Platform$OS.type
Example: Print the Operating-System Type in R
In the following example, we shall print the Operating System of this computer.
> print (.Platform$OS.type)
[1] "windows"
The result "windows" means that the current R session is running on a Windows platform. On Linux and macOS, this expression normally returns "unix".
Possible Values Returned by .Platform$OS.type
There are two possible values for .Platform$OS.type. They are:
- windows
- unix
The value "unix" represents Unix-like systems. It does not distinguish Linux from macOS. Use Sys.info() or R.version$os when your program needs more detailed platform information.
Check Whether R Is Running on Windows or Unix
You can compare .Platform$OS.type with a character value inside an if statement.
if (.Platform$OS.type == "windows") {
print("R is running on Windows")
} else {
print("R is running on a Unix-like operating system")
}
This approach is useful when a script needs to choose different path conventions, commands, or external executable names for Windows and Unix-like systems.
Get Detailed Operating-System Information with Sys.info()
The Sys.info() function returns a named character vector with details about the current system. Depending on the operating system, it may include the system name, release, version, machine architecture, node name, login name, and user name.
system_info <- Sys.info()
print(system_info)
To retrieve only the operating-system name reported by the system, access the sysname element.
Sys.info()[["sysname"]]
Typical values include "Windows", "Linux", and "Darwin". Darwin is the underlying Unix system used by macOS.
Get the R Platform and Operating-System Description
The R.version object contains information about the current R build. Its os and platform components can help identify the operating system and processor architecture for which R was built.
R.version$os
R.version$platform
You can also use R.version.string when you need the installed R version as a readable string, although it is not intended as the primary method for operating-system detection.
Detect Windows, Linux, and macOS Separately in R
Because .Platform$OS.type groups Linux and macOS under "unix", use Sys.info()[["sysname"]] when a script must distinguish among Windows, Linux, and macOS.
os_name <- Sys.info()[["sysname"]]
if (os_name == "Windows") {
print("Windows")
} else if (os_name == "Linux") {
print("Linux")
} else if (os_name == "Darwin") {
print("macOS")
} else {
print(paste("Other operating system:", os_name))
}
Use this method only when the distinction is necessary. For many scripts, platform-independent functions such as file.path() are preferable to separate operating-system branches.
Build File Paths Without Operating-System Checks
Operating systems use different path separators, but an R script usually should not concatenate paths manually. Use file.path() so that R constructs a suitable path for the current platform.
data_file <- file.path("data", "reports", "sales.csv")
print(data_file)
The platform-specific file separator is also available as .Platform$file.sep, while the separator used in environment path lists is available as .Platform$path.sep.
Uses of Knowing the Operating System Programmatically in R
If the developer knows which Operating System the program is running programmatically, operating system specific R code can be written.
- Selecting an operating-system-specific external command or executable.
- Handling platform-dependent environment variables.
- Choosing a native library or binary file compiled for the current system.
- Applying different configuration defaults for Windows, Linux, or macOS.
- Diagnosing environment details in logs and support reports.
Where possible, prefer R functions that already handle platform differences. Functions such as file.path(), tempdir(), normalizePath(), and Sys.getenv() often remove the need for manual operating-system checks.
R Operating-System Detection Checklist
- Use
.Platform$OS.typeonly when a Windows-versus-Unix distinction is sufficient. - Use
Sys.info()[["sysname"]]when Linux and macOS must be distinguished. - Account for
Sys.info()elements that may be unavailable on some systems. - Prefer
file.path()instead of manually inserting slash or backslash characters. - Avoid relying on display text from
R.version.stringfor program logic.
Conclusion
Use .Platform$OS.type to determine whether R is running on Windows or a Unix-like system. Use Sys.info() when you need to distinguish Windows, Linux, and macOS or retrieve additional system details. For portable scripts, use R’s platform-independent path and system functions whenever possible.
In this R Tutorial, we learned how to find Operating System of the computer programmatically.
TutorialKart.com