Package installation difficulties

Hi there. Trying to install packages and for some (knitr example below), I'm receiving the "binary source needs_compilation" error, and the packages do not install (even though it says that it has) whether I hit yes or no. On the MacOS Catalina and the latest version of R and RStudio (updated everything to try and fix) and have XQuartz downloaded.

Error message below

install.packages("knitr")
also installing the dependency ‘xfun’

There are binary versions available but the source versions are later:
binary source needs_compilation
xfun 0.14 0.15 TRUE
knitr 1.28 1.29 FALSE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) no
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/xfun_0.14.tgz'
Content type 'application/x-gzip' length 197792 bytes (193 KB)
==================================================
downloaded 193 KB

The downloaded binary packages are in
/var/folders/hg/fz6vc83x46n6qlwpnh4k01yw0000gn/T//Rtmp811Ucz/downloaded_packages
installing the source package ‘knitr’

trying URL 'https://cran.rstudio.com/src/contrib/knitr_1.29.tar.gz'
Content type 'application/x-gzip' length 885623 bytes (864 KB)
==================================================
downloaded 864 KB

  • installing source package ‘knitr’ ...
    ** package ‘knitr’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** R
    ** demo
    ** inst
    ** byte-compile and prepare package for lazy loading
    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
    namespace ‘xfun’ 0.14 is being loaded, but >= 0.15 is required
    Calls: ... namespaceImportFrom -> asNamespace -> loadNamespace
    Execution halted
    ERROR: lazy loading failed for package ‘knitr’
  • removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/knitr’
    Warning in install.packages :
    installation of package ‘knitr’ had non-zero exit status

The downloaded source packages are in
‘/private/var/folders/hg/fz6vc83x46n6qlwpnh4k01yw0000gn/T/Rtmp811Ucz/downloaded_packages’

In this particular case you are being asked to update xfun package, Do you get any error message if you try to update it?

install.packages("xfun")

I can install xfun (not compiling it) but this is still the 0.14 version I think. When I try to update it to 0.15, I get an error.

install.packages("xfun")

There is a binary version available but the source version is later:
binary source needs_compilation
xfun 0.14 0.15 TRUE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) no
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/xfun_0.14.tgz'
Content type 'application/x-gzip' length 197792 bytes (193 KB)
==================================================
downloaded 193 KB

The downloaded binary packages are in
/var/folders/hg/fz6vc83x46n6qlwpnh4k01yw0000gn/T//RtmpwDuL6L/downloaded_packages

Trying to update:

update.packages("xfun")
install.packages("xfun")

There is a binary version available but the source version is later:
binary source needs_compilation
xfun 0.14 0.15 TRUE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) Yes
installing the source package ‘xfun’

trying URL 'https://cran.rstudio.com/src/contrib/xfun_0.15.tar.gz'
Content type 'application/x-gzip' length 74045 bytes (72 KB)
==================================================
downloaded 72 KB

  • installing source package ‘xfun’ ...
    ** package ‘xfun’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs
    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    ERROR: compilation failed for package ‘xfun’
  • removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/xfun’
  • restoring previous ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/xfun’
    Warning in install.packages :
    installation of package ‘xfun’ had non-zero exit status

The downloaded source packages are in
‘/private/var/folders/hg/fz6vc83x46n6qlwpnh4k01yw0000gn/T/RtmpwDuL6L/downloaded_packages’

If you need to install the latest version from source, then you need to install Xcode in your system (not in R) and the recommended development tools for macOS systems.
https://cran.r-project.org/bin/macosx/tools/

Here is I solved this.
Went to the CRAN site for xfun and downloaded version .14 in the Mac binary "r-release" area. Installed it in RStudio by selecting package archive file option.

Then went to the CRAN site for knitr and downloaded the 1.28 version in the Mac binary "r-release:" area. Installed as above and I was able to install RMarkdown and create files.

For every Mac Update you have to install xcode-select.

Run this code in the Terminal (not R console):

xcode-select --install