You will need the XCode app from the App Store. When it is finally finished (it may take an hour depending on connection speed, etc.), open a terminal window
$ xcode-select --install
and then you may be able to install R packages that require compiling from source. Whenever you run install.packages("something") and you are greeted by "a binary version is available but a source version is later", you may, if you choose, select the option to install from source and that might work. If, however, it does not work the first time there is little chance it will work on a second attempt.