I personally really appreciate the InstallR package from Tal galilli, since it lets you install a great number of tools needed for working with R just running a function.

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.

Mac version of updateR function: the UpdateR package

That is why I developed updateR, a package that let’s you update your R version from R itself just running the following line of code:

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[[2]] # 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")

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


36 thoughts on “UpdateR package: update R version with a function (on MAC OSX)

  1. Hi Andrea,
    I followed all the steps and all of them worked fine but for the last one (updateR(admin_pas….)). I am a sudoer on my imac. I get the following error:
    updateR(admin_password = ‘XXXXXXXX’)
    Error in open.connection(x, “rb”) : cannot open the connection

      1. Ofcourse, I did gave my password in place of XXXXs. For security reason I indicated the entry as XXXX

      2. Ok ok 😅 would you mind to post the error on the github repository? We are working there on the issue tracking. Thanks: github.com/andreacirilloac/updateR

  2. Andrea, all seems to work well except that I am being updated to 3.3.1 which I was already running. I was hoping to be updated to 3.3.2 as, apparently, that is what is needed to run one of today’s Rblogger’s exercises on tidyverse.

  3. Hi Andrea, I get the error ‘Error in UseMethod(“html_nodes”) :
    no applicable method for ‘html_nodes’ applied to an object of class “c(‘xml_document’, ‘xml_node’)’
    Any ideas what might be wrong? Thanks -Liz

  4. Hi Andrea,

    I haven’t updated R for a while, and I came across your package today, but I can’t seem to get it to work. I’m getting the “Everything went smoothly” message, but the line before it reads:

    R was updated to R version 3.2.3 (2015-12-10) — “Wooden Christmas-Tree”

    This is the version that I’ve had for some time now, and obviously isn’t the latest version. There aren’t any error messages or any problem executing the call. Do you have any idea if I’m doing something wrong on my part that is resulting in the update not actually updating my current version?

    Thanks for your help!

  5. For some reason it doesn’t update, even though it downloaded the new version.
    When running the updateR function I get the following…

    trying URL ‘https://cran.rstudio.com/bin/macosx/R-3.3.2.pkg’

    Content type ‘unknown’ length 74323165 bytes (70.9 MB)

    downloaded 70.9 MB

    R was updated to R version 3.3.1 (2016-06-21) — “Bug in Your Hair”
    Everything went smoothly!

    1. Hi Gordon , thank you for your feedback. We are currently tracking issues on GitHub : andreacirilloac/updateR . Could you kindly open an issue there? It would be very kind of you. Thanks!

  6. I think we should change read_html to html. The following works fine to me

    page_source = “https://cran.rstudio.com/bin/macosx/”
    stopifnot(.Platform$OS.type == “unix”)
    page <- rvest::html(page_source)
    version_block a”)
    filename % strsplit(“\n”,
    fixed = TRUE)
    filename <- filename[[2]]
    filename_quoted <- paste(“‘”, filename, “‘”, sep = “”)
    assertthat::stopifnot(grepl(“.pkg”, version) == FALSE)
    url <- paste(page_source, filename, sep = “”)
    download.file(url, filename)
    command <- paste(“echo “, admin_password, “| sudo -S installer -pkg “,
    filename_quoted, ” -target / -verbose”)
    message(“everything went smoothly”)
    message(“open a Terminal session and run ‘R’ to verify that latest version was installed”)

    1. hi,
      really thank you for commenting.
      I would actually avoid employing html() since it appears being deprecated from rvest maintainer (just run ?html to be sure). Nevertheless the bug you were pointing out was a real one: read_html() is reexported from xml2 ( more here: https://github.com/hadley/rvest/issues/109). I fixed it adding dependence on xml2 and substituting rvest:: with xml2::.
      asserthate namespace error got fixed as well. please refere to my answer to Nathan for further on this.
      Finally, here it comes the session info you asked for:
      R version 3.3.0 (2016-05-03)
      Platform: x86_64-apple-darwin13.4.0 (64-bit)
      Running under: OS X 10.12.2 (unknown)

      [1] it_IT.UTF-8/it_IT.UTF-8/it_IT.UTF-8/C/it_IT.UTF-8/it_IT.UTF-8

      attached base packages:
      [1] stats graphics grDevices utils datasets methods base

      loaded via a namespace (and not attached):
      [1] httr_1.2.0 assertthat_0.1 magrittr_1.5 R6_2.1.2 htmltools_0.3.5 tools_3.3.0 Rcpp_0.12.5
      [8] xml2_1.0.0 digest_0.6.9 rvest_0.3.2

      thank you once again for commenting.


  7. could you please provide a session_info for your testing package? I confronted all kinds bugs like:

    Error: ‘stopifnot’ is not an exported object from ‘namespace:assertthat’
    Error: ‘read_html’ is not an exported object from ‘namespace:rvest’

  8. Hi Andrea. After running updateR for the first time, I came across the following:

    Attaching package: ‘dplyr’

    The following objects are masked from ‘package:stats’:

    filter, lag

    The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

    Error: ‘stopifnot’ is not an exported object from ‘namespace:assertthat’

    Can you help?

    1. Hi Nathan,

      first of all thank you for commenting. I do apologise for late reply but I am working on a new book and they were quite busy days :).
      the problem you were encountering should be related to an erroneous export of stopifnot function from assert hat package. it should now be fixed.
      I would really appreciate if you could give it another try and come back with another feedback.

      thank you once more for commenting!

  9. Hi Andrea. I ran the following commands in sequence:
    updateR(admin_password = “mypasswd”)

    However I’m having this output:
    Error: could not find function “updateR”

    Can you help? And thanks a bunch for creating this script.

  10. Hi Andrea,

    The package looks very cool. However, I am getting the same error message as George, and the workaround you mentioned is not working for me.

    Any suggestions?

  11. Hi Andrea,
    Thanks so much for this contribution! I ran your line of script:

    install.packages(‘devtools’) #assuming it is not already installed
    updateR(admin_password = ‘Admin user password’)

    Everything ran well until it came time to updateR. In return, I received the error message:

    updateR(admin_password = ‘Admin user password’)
    Error in updateR(admin_password = “Admin user password”) :
    could not find function “read_html”

    I used my admin password to allow for the download and received the same error code: “could not find function ‘read_html'”.

    Any ideas on how to get past this error code?

    Thanks so much!

    Mariah P.

  12. Hi Andrea,

    It’s really great that you created an installer package for Mac users. I was about the update R, and remembered how painful the update can get in terms of accessing the libraries… So I was kind of looking for a simple way that I could just run from the command line and wouldn’t be painful, and would simply install R nice and easy… I gave your package a try, had no problems, although it seems like the library issue still isn’t solved — I guess I was expecting a little too much from the package 🙂

    Couple of questions/suggestions:

    In my first try I didn’t realize I had a typo with my admin password, yet the function ran and didn’t give me an error. It even said

    “everything went smoothly
    open a Terminal session and run ‘R’ to verify that latest version was installed”

    But R obviously wasn’t installed, yet the R-.pkg was downloaded. Maybe it would be a good thing to have another stopifnot type of line included at the begining to prevent the user from downloading if the user’s password is incorrect and throw an error? And/Or have another function in the package to finish up the installation, if the R-.pkg is already downloaded?

    I believe R-*.pkg is simply downloaded to the working directory? Prompting a message after the download to remind that can be helpful.
    I still wanted to try the package so removed the R-*.pkg from the terminal, and reran updateR, but this second time I didn’t get a prompt to select the cran repo, what would the user do if they want to change the repo the next time that they use updateR again?

    Thanks for the contribution!

    1. Really thank you for taking the time to give me such a constructive and detailed feedback. Would you mind to open issue/issues on GitHub repo so that we can track and solve them properly.
      Going to fix highlighted issues better structuring error handling.
      That said could you kindly confirm me if:
      – you had no issues regarding package dependencies on other packages
      – the R-3...pkg was properly downloaded
      – finally , after fixing the password typo managed to update your R version.
      Thank you once again for your precious feedback!

  13. As you mentioned,
    “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”

    Same happened with me, but my R version is still earlier one, not the latest one.
    Am i missing something?

    Even version command also showing me earlier version not the updated one

    1. First of all I am really sorry for this late reply. Thank you for commenting.
      I have been working on updateR recently and I have pushed an updated version to GitHub, which should fix the issue you were encountering.
      I would be pleased if you could test it on your OS and give me a feedback.

  14. Hi Andrea, ran your instructions for updating R from within RStudio on Mac OS X, but received an error msg “Error: ‘stopifnot’ is not an exported object from ‘namespace:assertthat'” appreciate your help thanks.

    1. Hi George,
      Thank you for reading and commenting!

      The error is due to some problems on package dependencies, I am currently working on it.

      In the meanwhile you should be able to workaround it installing assertthat package:


      Going to update you asap.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s