Installing sf with brew

While in the past I have used conda for managing my R environments for individual courses, for a spatial statistics course I am taking this semester I decided to use just a bare metal R installation. This has a couple advantages, like being able to run natively on my M1 chip or also being able to link the `*.h` files of R packages for other work, and so far I haven't ran into issues when using it. However, today when attempting to install the Simple Features (SF) library, I finally ran into some issues with trying to link to existing geospatial libraries:

checking GDAL: checking whether PROJ is available for linking:… yes

checking GDAL: checking whether PROJ is available fur running:… yes

configure: GDAL: 3.6.2

configure: pkg-config proj exists, will use it

configure: using proj.h.

checking PROJ: checking whether PROJ and sqlite3 are available for linking:… no

configure: error: libproj or sqlite3 not found in standard or given locations.

ERROR: configuration failed for package ‘sf’

/* removing ‘/opt/homebrew/lib/R/4.2/site-library/sf’

Using `pkg-config` I can see that I do have all the required libraries

$ pkg-config sqlite3 gdal proj geos --libs --cflags

## Returns
-I/opt/homebrew/Cellar/gdal/3.6.2/include
-I/opt/homebrew/Cellar/proj/9.1.1/include
-I/opt/homebrew/Cellar/libtiff/4.4.0_1/include
-I/opt/homebrew/Cellar/geos/3.11.1/include
-L/opt/homebrew/Cellar/gdal/3.6.2/lib
-L/opt/homebrew/Cellar/proj/9.1.1/lib
-L/opt/homebrew/Cellar/geos/3.11.1/lib
-lsqlite3 -lgdal -lproj -lgeos_c

So while I am not entirely sure why SF install is unable to properly link the correct proj version, to fix it is just a matter of specifying the configuration arguments when installing the package. For the sake of future usage so I don't have to change the proj path and version, I used `pkg-config` to pull the paths and just passed them to `install.packages` in the shell.

  proj_lib=`
      pkg-config proj --cflags |
      cut -d ' ' -f1 |
      cut -c 3-`
  proj_inc=`
      pkg-config proj --libs |
      cut -d ' ' -f1 |
      cut -c 3-`

  Rscript -e "
  install.packages(
    'sf',
    repos='http://cran.us.r-project.org',
    configure.args = '--with-proj-include=${proj_inc}
                      --with-proj-lib=${proj_inc}'
  )
  "
> library(sf)
Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE

The above code succesfully compiles SF and I haven't ran into any issues when using the library. When I get time I'll look into why the normal method can't link to the proper headers.

2023.03.07 - update

The same issue occurs when attempting to install Terra. The same solution that was used for simple features works to succesfully install.

proj_lib=`pkg-config proj --cflags |  cut -d ' ' -f1 | cut -c 3-`
proj_inc=`pkg-config proj --libs |  cut -d ' ' -f1 | cut -c 3-`

Rscript -e "
install.packages(
  'terra',
  repos='http://cran.us.r-project.org',
  configure.args = '--with-proj-include=${proj_inc} --with-proj-lib=${proj_inc}'
)