System Calls & Commands in R (2 Examples)

 

In this R tutorial I’ll give you a brief overview about apply functions that are returning information from the operating system.

The article consists of this content:

Let’s dive right into the tutorial.

 

Example 1: Handle Dates & Times Using System Functions

This section demonstrates how to return information and manipulate dates and times in the R programming language.

There are basically three functions that can be used to extract the current date and time from the system.

The Sys.Date function extracts the current date…

Sys.Date()                           # Get system date
# [1] "2021-10-12"

…the Sys.time function returns the current time…

Sys.time()                           # Get system time
# [1] "2021-10-12 09:52:30 UTC"

…and the Sys.timezone function identifies the timezone corresponding to this date and time:

Sys.timezone()                       # Get system timezone
# [1] "Etc/UTC"

It is also possible to modify those times and time zones. You may change the default time zone (see this list of known time zones), you may set a particular date and time for a file or a directory using the Sys.setFileTime function, and you may use functions such as Sys.sleep to measure the execution time of an R code, or to pause the execution of an R code.

So as you have seen in this section, the system commands provided by the R programming language provide multiple ways on how to deal with times and dates in R.

 

Example 2: Get Information About System & R Version

System calls and functions can also be used to get detailed information about the currently used system.

The Sys.info function returns the name of the operating system and the name of the current user…

Sys.info()                           # Get system information
#           sysname           release           version          nodename 
#         "Windows"          "10 x64"     "build 19043" "DESKTOP-FTRQE9D" 
#           machine             login              user    effective_user 
#          "x86-64"           "Joach"           "Joach"           "Joach"

…the Sys.localeconv function provides information on the numerical and monetary representations that are currently used (see here for instructions on how to change the locale)…

Sys.localeconv()                     # Numerical & monetary representations
#     decimal_point     thousands_sep          grouping   int_curr_symbol 
#               "."                ""                ""             "EUR" 
#   currency_symbol mon_decimal_point mon_thousands_sep      mon_grouping 
#               "€"               ","               "."            "\003" 
#     positive_sign     negative_sign   int_frac_digits       frac_digits 
#                ""               "-"               "2"               "2" 
#     p_cs_precedes    p_sep_by_space     n_cs_precedes    n_sep_by_space 
#               "0"               "1"               "0"               "1" 
#       p_sign_posn       n_sign_posn 
#               "1"               "1"

… the Sys.getpid function returns the processing ID…

Sys.getpid()                         # Get process ID
# [1] 19076

…and the R.Version function can be applied to return information on the currently used version of R:

R.Version()                          # Information about R version
# $platform
# [1] "x86_64-w64-mingw32"
# 
# $arch
# [1] "x86_64"
# 
# $os
# [1] "mingw32"
# 
# $system
# [1] "x86_64, mingw32"
# 
# $status
# [1] ""
# 
# $major
# [1] "4"
# 
# $minor
# [1] "1.0"
# 
# $year
# [1] "2021"
# 
# $month
# [1] "05"
# 
# $day
# [1] "18"

This section has explained how to extract all kinds of info on the currently used system as well as on the R and RStudio versions. However, there is more to explore!

 

Example 3: Find & Complete File & Directory Paths

Other useful applications of system calls and commands are provided when it comes to the handling of file and directory paths.

Two useful examples are the Sys.glob function that can be used to perform wildcard expansions on file paths…

Sys.glob(file.path("C:/Users/Joach/Desktop/my directory", "*", "*.txt"))  # Globbing
# [1] "C:/Users/Joach/Desktop/my directory/folder 1/my file 1.txt"
# [2] "C:/Users/Joach/Desktop/my directory/folder 1/my file 2.txt"
# [3] "C:/Users/Joach/Desktop/my directory/folder 2/my file 3.txt"
# [4] "C:/Users/Joach/Desktop/my directory/folder 3/my file 4.txt"

…and the system.file function that identifies the location of system files (i.e. the path to the stringr package):

system.file(package = "stringr")                                          # Package location
# [1] "C:/Users/Joach/Documents/R/win-library/4.1/stringr"

Furthermore, you may have a look at this tutorial, which explains how to handle file permissions using the Sys.chmod function.

Long story short: System functions are great! 🙂

 

Video, Further Resources & Summary

In case you need further explanations on the examples of this article, I can recommend watching the following video on my YouTube channel. In the video, I’m explaining the R programming syntax of this tutorial.

 

The YouTube video will be added soon.

 

In this R programming article you have learned how to apply different system functions. In case you have any further questions, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top