---
title: "Issues with `{sf}` when installing with `{renv}`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{renv-issues}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r, eval=FALSE, include=FALSE}
renv::purge("sf")
renv::remove("sf")
renv::purge("dawaR")
renv::remove("dawaR")

reprex::reprex({
  options(repos = c(CRAN = "https://packagemanager.posit.co/cran/latest"))
  renv::install("sf")
  renv::install("aleksanderbl29/dawaR")
  dawaR::get_map_data("regioner")
})
```

This vignette is intended to help resolve issues when installing `{dawaR}` on MacOS in a [`renv`](https://rstudio.github.io/renv/index.html) environment. We will resolve the issue that is printed below.

``` r
renv::install("aleksanderbl29/dawaR")
#> # Downloading packages -------------------------------------------------------
#> - Downloading sf from CRAN ...                  OK [18.6 Mb in 1.8s]
#> - Downloading dawaR from GitHub ...             OK [543.6 Kb in 0.67s]
#> Successfully downloaded 2 package in 2.5 seconds.
#> 
#> The following package(s) will be installed:
#> - sf [1.0-17]
#> - dawaR [aleksanderbl29/dawaR@main]
#> These packages will be installed into "~/Downloads/test/renv/library/macos/R-4.4/aarch64-apple-darwin20".
#> 
#> # Installing packages --------------------------------------------------------
#> - Installing sf ...                             OK [installed binary and cached in 0.72s]
#> - Installing dawaR ...                          OK [built from source and cached in 0.72s]
#> Successfully installed 2 package in 1.44 seconds.
dawaR::get_map_data("regioner")
#> Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
#> Error 1: PROJ: proj_create_from_database: Cannot find proj.db
#> Simple feature collection with 5 features and 9 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 8.07251 ymin: 54.55908 xmax: 15.1974 ymax: 57.75257
#> Geodetic CRS:  WGS 84
#>   dagi_id kode               navn nuts2              ændret          geo_ændret
#> 1  389098 1081 Region Nordjylland  DK05 2024-02-05 23:01:25 2024-02-05 23:01:25
#> 2  389101 1082 Region Midtjylland  DK04 2024-02-05 23:01:25 2024-02-05 23:01:25
#> 3  389102 1083  Region Syddanmark  DK03 2024-02-05 23:01:25 2024-02-05 23:01:25
#> 4  389099 1084 Region Hovedstaden  DK01 2024-02-05 23:01:25 2024-02-05 23:01:25
#> 5  389100 1085    Region Sjælland  DK02 2024-01-16 23:03:35 2024-01-16 23:03:35
#>   geo_version visueltcenter_x visueltcenter_y                       geometry
#> 1          31       10.112829        57.30716 MULTIPOLYGON (((10.6141 57....
#> 2          41        9.605032        56.23399 MULTIPOLYGON (((9.06696 56....
#> 3          31        9.028461        55.56317 MULTIPOLYGON (((10.53094 54...
#> 4          30       12.279374        55.97239 MULTIPOLYGON (((12.31077 56...
#> 5          26       11.621319        55.43979 MULTIPOLYGON (((11.9013 54....
```

Specifically the issue with `GDAL` not being able to find `proj.db`.

According to [this issue on GitHub](https://github.com/rstudio/renv/issues/1832) the issue is caused by `renv` installing the version of `{sf}` from "https://packagemanager.posit.co/cran/latest" where the `gdal` subdirectory is not included in the `{sf}` package from this repo - but it is included in the version of `{sf}` from "https://cran.rstudio.com/". To work around this you should set the CRAN repo before installing the package.

You can do as follows:

```{r, eval=FALSE}
options(repos = c(CRAN = "https://cran.rstudio.com/"))
renv::remove("sf")
renv::purge("sf")
renv::install("sf")
renv::install("aleksanderbl29/dawaR")
renv::snapshot()
renv::clean() # for good measure
renv::rebuild()
```

Now all should be hunky-dory! Have fun with the package.
