R is (Almost) Platform Independent
[This article was first published on Strategic Thinking - Automated In R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I write R scripts on both my laptop and desktop, so the main issue I have is making sure that the R scripts are updated on these devices. There are several ways to ensure this happens:
- Use a version control system (on the cloud), e.g Github
- Write R scripts on an RStudio server, which is accessible on any web browser
- Transfer files via FTP or using a remote desktop software, such as Teamviewer
The first method has an advantage that you can perform a diff of changes made to any file after it is checked in to the cloud, and these changes can be pulled (or merged) from any devices. I perform only basic functions, such as check in/out, merge/pull, sync although it did once save me from a disaster as I had to roll back several versions to get to a previous stable version of code.
The second method is used when I am currently working on a project, but the R script is not stable yet to be checked-in. The RStudio GUI is available on a web browser and I can continue working on ANY devices. Also, an advantage of having an RStudio server is that I can run R scripts as a background process. For example, I currently have TWO (2) background scripts: (1) the first is a shiny app that I can access using a web browser; and (2) the second is an R app which periodically checks for new files to download and it automatically emails the files as attachment to my email address. However, the setup of RStudio server on Ubuntu was arduously long, and I may write a how-to blog post on this for newbies.
The third method is rather trivial, as FTP and RDP protocols are very common. Suffice to say, that I can easily transfer files between my laptop and device once the software has been installed.
Although R is platform independent, however the functions that access the underlying system are NOT. For example, the function setwd() requires a path as input that depends on the OS, e.g. Windows path start with “C:/”, but Linux path starts with “~/”. Rather than dealing with these OS-specific details each time I access the system, I decided to create a library of functions to deal with this.
Some query functions are: RegIsWindowsBln() and RegIsLinuxBln() returns true/false depending on the OS.
RegIsWindowsBln <- function() {
retBln <- (Sys.info()["sysname"] == "Windows")
names(retBln) <- NULL
retBln
}
RegIsLinuxBln <- function() {
retBln <- (Sys.info()["sysname"] == "Linux")
names(retBln) <- NULL
retBln
}
Some system functions are: RegGetHomeDir() and RegGetRDir() returns paths that are specific to the OS, e.g. “C:\Users\myname” for Windows.
RegGetHomeDir <- function() {
retDir <- NULL
if( RegIsLinuxBln() )
retDir <- paste0("/home/",Sys.info()["user"],"/")
if( RegIsWindowsBln() )
retDir <- paste0("C:/Users/",Sys.info()["user"],"/")
retDir
}
Shell command: RegSystemNum() calls the R function system() with different parameters that are OS-specific and returns an error number.
RegSystemNum <- function(cmdChr) {
if( RegIsLinuxBln() )
errNum <- tryCatch( system(cmdChr, intern=FALSE, wait=TRUE),
error=function(e) { 9999 }, finally={} )
if( RegIsWindowsBln() )
errNum <- tryCatch( system(cmdChr, show.output.on.console=FALSE,
intern=FALSE, wait=TRUE),
error=function(e) { 9999 }, finally={} )
errNum
}
Also, I extended this library to include some type checking function: RegIsEmailBln() and returns true/false if email format is valid or invalid.
RegIsEmailBln <- function( emailChr ) {
patStr <- "^([a-zA-Z0-9]+[a-zA-Z0-9._%-]*@(?:[a-zA-Z0-9-])+(\\.+[a-zA-Z]{2,4}){1,2})$"
grepl(patStr, emailChr)
}
I will stop here and I hope this post has encouraged some of you readers to create and extend this library of functions.
To leave a comment for the author, please follow the link and comment on their blog: Strategic Thinking - Automated In R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.