Little useless-useful R functions – R Version

[This article was first published on R – TomazTsql, 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.

Printing out R version is absolute no fun. So making it fun using the following useless-useful R function will make this little bit more interesting.

Should be a print of “Hello R” with the version.

Yes, I am rocking an older version of R, but this was to test the behaviour of different R Versions and operating systems (works on Windows and MacOS system).

The function is fairly simple, as it uses unserialize() function in base R. In addition, I am adding little bit of dplyr to

HelloRversion <- function(text=TRUE){

  if (text==TRUE){ 
    # Get some text
    text_R <- "580a000000030003060300030500000000055554462d38000000100000000100040009000001284820202048204545454545204c20202020204c2020202020204f4f4f2020202020202052525252202020202121200a4820202048204520202020204c20202020204c20202020204f2020204f20202020202052202020522020202121200a4848484848204545454545204c20202020204c20202020204f2020204f20202020202052525252202020202121200a4820202048204520202020204c20202020204c20202020204f2020204f202020202020522020205220202020200a4820202048204545454545204c4c4c4c4c204c4c4c4c4c20204f4f4f20202020202020522020205220202021210a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d0a202020202020202020207c207665723a20"
  } else {
    text_R <- "580a000000030003060300030500000000055554462d380000001000000001000400090000021d202020202020202020202020202020202020202020202020202020202020202020205f5f0a202020202020202020202020202020202020202020202020202020202020205f2e2d7e2020290a20202020202020202020202020202020202020205f2e2e2d2d7e7e7e7e2c272020202c2d2f20202020205f0a20202020202020202020202020202020202e2d272e202e202e202e272020202c2d272c27202020202c2720290a2020202020202020202020202020202c272e202e202e205f2020202c2d2d7e2c2d275f5f2e2e2d2720202c270a202020202020202020202020202c272e202e202e202028402927202d2d2d7e7e7e7e2020202020202c270a2020202020202020202020202f2e202e202e202e20277e7e202020202020202020202020202c2d270a20202020202020202020202f2e202e202e202e202e202020202020202020202020202c2d270a202020202020202020203b202e202e202e202e20202d202e20202020202020202c270a2020202020202020203a202e202e202e202e202020202020205f20202020202f0a20202020202020202e202e202e202e202e20202020202020202020602d2e3a0a202020202020202e202e202e202e2f20202d202e20202020202020202020290a2020202020202e20202e202e207c20205f5f5f5f5f2e2e2d2d2d2e2e5f2f200a7e2d2d2d7e7e7e7e2d2d2d2d7e7e7e7e202020202020202020202020207e7e0a2020202020207c207665723a20"
  }  
  
  # Get R version
  vR<- trimws(gsub("\\(.*?\\)", "", sub("R version ","",R.version$version.string)))
  
    
  unserialized_vR <- text_R %>% 
      {substring(., seq(1, nchar(.), 2), seq(2, nchar(.), 2))} %>% 
          paste0("0x", .) %>% 
           as.integer %>% 
            as.raw %>% 
             unserialize()
  
  unserialized_vR <- paste0(unserialized_vR,vR,' |')
  
  
  cat("\014")   
  #cat("\f") if running on Windows OS
  cat(unserialized_vR)
}

The serialized text is essentially converted ASCII art and is outputted on the console along with the R engine version.

To run the function, simply run:

HelloRversion()

But if you want to get creative, run the function argument as FALSE:

HelloRversion(FALSE)

And get the little dolphin as ASCII creature.

<<< Not giving you the picture, go check the code for yourself! >>>>

Making versions fun, the code is available at the Github in same Useless_R_function repository.

Happy R coding and stay safe!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)