- Mac version of updateR function: the UpdateR package
- how to install the updateR package
- how to update R version using the updateR package
- behind the scenes: how updateR works
Among these functions one of my favourite is the updateR() function which checks for new versions of R language and in case of positive response installs it on your machine. The only issue with this function is that it works only on Windows Operating systems.
UPDATE: in the coming weeks I am going to merge UpdateR into installR to better serve the R community. Be sure to subscribe to the newsletter to be aknowledged when it will happen and avoid issues.
Mac version of updateR function: the UpdateR package
Since I am a lazy programmer and generally a devote of the great thesis introduced within The pragmatc programmer, I decided to develop updateR, a package that let’s you update your R version from R itself just running one line of code.
And here it is the simple line of code you have to run:
updateR(admin_password = "os_admin_user_password")
that is to say you will only have to provide admin user password. This password is required because some of the command executed by updateR() are run as super user (for advanced readers we are talking of our old “sudo” friend).
short paragraphs which follow shows you how to install and use the package.
how to install the updateR package
UpdateR package is hosted on Github. You will therefore have to install it leveraging the install_github() function from devtools package:
how to update R version using the updateR package
As seen, updating R version from R is actually a breeze: you just need to run a function.
all you will need to do is retrieve your MAC admin user password and then run the two following lines of code:
updateR(admin_password = "os_admin_user_password")
Since every command launched in background (see below for more infos) is passed with verbose option on, you will suddenly see your console being populated of output messages informing you on how things are going on.
A the end of the installation process those two lines should appear:
everything went smoothly open a Terminal session and run 'R' to assert that latest version was installed
that is all you need to know to use updateR. By the way If you want to know more on how the function works, have a look at the following paragraphs.
behind the scenes: how updateR works
as experimented with Ramazon package, which allows you to publish your shiny app on amazon AWS with a function, I developed updateR mainly leveraging the system() function, which lets you pass commands to the command line directly from the R environment.
That said, the updateR function workflow can be summarized into the following steps:
verify that user is running a unix machine
as said, updateR is a package thought as a OSX twin of Tall galili installr package and can therefore be run only on Mac OSX machines.
Following this updateR takes care of verifying that you are launching it from a unix OS, running the stopifnot() function in order to alt the execution on non-unix systems:
stopifnot(.Platform$OS.type == "unix")
get last R version from CRAN
How would you programmatically check for the latest R version?
updateR does it importing in R the html code of the rstudio CRAN mirror page (https://cran.rstudio.com/bin/macosx/) and extracting from it the version number.
page <- xml2::read_html(page_source) version_block <- rvest::html_nodes(page,"h1+ p a+ a , table:nth-child(8) tr:nth-child(1) td > a") filename <- rvest::html_text(version_block) %>% strsplit("\n", fixed = TRUE) # the resulting value is a list filename <- filename[] # we take the second element, containing the name of the file filename_quoted <- paste("'",filename,"'",sep = "")
Be aware that resulting filename object will be a string similar to the following :“R-xx.xx.xx.pkg”, since we are looking to compose a download URL for the version package on our machine.
This URL is composed pasting the CRAN mirror url togheter with the filename object:
url <- paste(page_source,filename, sep = "")
one thing still missing here is the comparison between version installed on the maching calling the function and the retrieved version on CRAN.
I will probably (hopefully) develop this within next releases, and collaboration on this would be really welcome.
After defining the download URL we can actually proceed with downloading the package and install it. the first of those two steps will be performed leveraging built-in function download.file() :
run command line commands within R
the last step is the actual installation of the R version package. this activity will be performed using the command line command installer .
we are first going to put toghethet our line of command with paste() function and then submit to the terminal calling system() function on our command string:
command <- paste("echo " , admin_password, "| sudo -S installer -pkg ", filename_quoted , " -target / -verbose") system(command)
accomplisments and further developments
updateR closed a gap standing in the way of Mac R users, providing an handful tool to update their version of the software. the good acceptance the package is obtaining is not that much suprising. It is currently cited as the preferred way to perform the update within different “official sources” of the R community. Tal Galili himself mentioned the package as an OSX alternative to his package. We are now planning to merge UpdateR into installR to better serve our beloved Rlovers.
The merge between updateR and installr should happen in the coming weeks. As we were saying you can subscribe to the newsletter to be aknowledged of the exact timing and avoid issues with your code.
feel free to complain with me
I shared updateR as a package since I think it could be a really useful tool for the R commuynity.
As R itself, updateR package comes with absolute no warranty ( ;)) , nevertheless I will be really pleased to read your comments and suggestions on it, as well as I will be pleased if you would like to further improve it through the Github repo