Skip to contents

Intro to scari R code

In this file, I will list the dependency packages for scari and initialize the renv package to keep my other package versions consistent. End users should only run sections 1 and 2. Section 3 is for troubleshooting common issues. Section 4 should only be used for the first-time initialization of the package.

Notes about using this package’s code

I use some of the following conventions to ensure that the package’s .html files render correctly, the code is not overly cumbersome to run, and that data aren’t re-downloaded unnecessarily:

  • I have added a number of if() statements throughout my vignettes, usually to prevent data from being downloaded again unnecessarily. Simply change the FALSE within the if() statement to TRUE to run a chunk.
  • I have a chunk near the beginning of every vignette that sets the global chunk options: knitr::opts_chunk$set(eval = FALSE). This prevents the code from running automatically when the vignette is rendered. You can change this to TRUE if you want to run the code in that chunk.
  • I have added html comment tags (<!-- and -->) to the code chunks that I do not want to run automatically as the website is rendered. Much of the code that is commented out is redundant, but I have left it in the code for reference so as not to disrupt the true workflow. You can remove these comments if you want to run the code in those chunks.
  • If you choose to run the code with updated data, you may need to occasionally change file names to match the names of the new datasets. Be aware that some vignettes use versions of file names (for example, MaxEnt models are versioned and should be updated with each run).
  • When generating images or files, most of the time I set overwrite = FALSE to avoid unnecessary mistakes. You should change these to true if you intend to do an entirely new run of the package overtop an old run.

Required Package Initialization

1. create necessary folders

I will create the folders where the outputs of this package will be saved. Note that a set of these folders will be 1 level above the root folder for the package (so it will go on your file system in the same folder as this package).

# data folder
dir.create(file.path(here::here(), "data"))
# output folders
dir.create(file.path(here::here(), "vignette-outputs", "data-tables"))
dir.create(file.path(here::here(), "vignette-outputs", "figures"))
dir.create(file.path(here::here(), "vignette-outputs", "figures", "figures-rds"))
dir.create(file.path(here::here(), "vignette-outputs", "shapefiles"))
dir.create(file.path(here::here(), "vignette-outputs", "reports"))
# folder 1 level above the package for maxent outputs and subfolders
dir.create(file.path(here::here() %>% dirname(), "maxent"))
# model storage
dir.create(file.path(here::here() %>% dirname(), "maxent", "models"))
dir.create(file.path(here::here() %>% dirname(), "maxent", "models", "working_dir"))
# historical rasters
dir.create(file.path(here::here() %>% dirname(), "maxent", "historical_climate_rasters"))
dir.create(file.path(here::here() %>% dirname(), "maxent", "historical_climate_rasters", "chelsa2.1_30arcsec"))
# future rasters
dir.create(file.path(here::here() %>% dirname(), "maxent", "future_climate_rasters"))
dir.create(file.path(here::here() %>% dirname(), "maxent", "future_climate_rasters", "chelsa2.1_30arcsec"))
dir.create(file.path(here::here() %>% dirname(), "maxent", "future_climate_rasters", "chelsa2.1_30arcsec", "2041-2070_ssp126_GFDL"))
dir.create(file.path(here::here() %>% dirname(), "maxent", "future_climate_rasters", "chelsa2.1_30arcsec", "2041-2070_ssp370_GFDL"))
dir.create(file.path(here::here() %>% dirname(), "maxent", "future_climate_rasters", "chelsa2.1_30arcsec", "2041-2070_ssp585_GFDL"))

2. Set GitHub Credentials

Run this chunk to set a GitHub PAT (Personal Access Token) in your R session, allowing R to communicate with GitHub for downloads. You will need to manually generate your own PAT- see link for details:

https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens

# you may need to enter your github credentials into the .Renviron or log into github for this to work:
usethis::edit_r_environ()
# then, create a github PAT on website. 
usethis::create_github_token()
# enter created PAT (personal access token) into the .Renviron
## be sure to use defaults and allow download and upload of packages
gitcreds::gitcreds_set()

3. one-time renv initialization anc check for package dependencies

Run this chunk to see the dependencies for the scari package.

packages <- renv::dependencies() %>%
  # select unique package names
  dplyr::summarize(package = sort(unique(Package))) %>%
  as.matrix() %>%
  as.character()

I created this section to initialize the renv package for the first time ONLY. Prior to running this chunk, you should build and install the scari package internally.

# ensure that developer packages are installed prior to activation
remotes::install_github("ieco-lab/lydemapr", build_vignettes = FALSE)
remotes::install_github("ropensci/rnaturalearthhires")
remotes::install_github("ropensci/rnaturalearthdata")
remotes::install_github("densitymodelling/dsmextra")
# if these continue to throw errors, you might exit the scari Rproject session and install them. Then, re-open the Rproject.

# please also install these specific package versions:
remotes::install_version("ggnewscale", version = "0.4.10") # due to issue with color scales
remotes::install_version("GeoThinneR", version = "1.1.0") # due to issue with thin_points
remotes::install_version("tidygeocoder", version = "1.0.5") # due to issue with reverse_geocode()

# initialization of project renv
# only run once
renv::init(project = file.path(here::here()))

# check status
renv::status()

4. Run package updates

You should run this chunk periodically (at least yearly) to update the packages in this project. This is especially important of some of the data packages, including lydemapr, so that you have the most up-to-date versions of the data. renv will not update packages in the lockfile unless told to specifically. It will also not update itself without being told.

# update all package dependencies for scari
renv::update()
# check for updates to renv itself
renv::upgrade()

Troubleshooting

Ensure R can find Git

If the above installation does not work, you may first need to have git installed, found here: https://git-scm.com/downloads. Then, open the windows command prompt and run the following code to ensure that R can find git. “git –version” should return the version of git installed on your computer. If it does not, you may need to adjust the system environmental variables by adding system environmental variables. Click on system variables > path and add the following paths:

C:\Program Files\Git\cmd C:\Program Files\Git\bin

Windows should be able to locate git now.

If R cant find git, run this code in Rstudio:

Sys.setenv(PATH = paste("C:/Program Files/Git/cmd", Sys.getenv("PATH"), sep = ";"))  # adjust path as needed

Retrieve correct dependencies for scari

# first, check the status of your current package versions vs those in renv
renv::status()

# you may need to call this if the dependencies are used and installed, but not recorded
renv::snapshot()
# you should run this to ensure the correct package versions are used while you are working within this project. This aligns the sys library with the lockfile.
renv::restore()
# only use this version if you want to edit your system library to match the renv library
renv::restore(clean = TRUE)

The first thing you should try is updating your R and Rtools versions. This messed up my renv for quite awhile.

# these were the packages initially giving errors for "missing description files"
remove.packages(c("lydemapr", "rnaturalearthhires", "rnaturalearthdata", "dsmextra"))
renv::remove(c("lydemapr", "rnaturalearthhires", "rnaturalearthdata", "dsmextra"))
#reinstall
renv::install(c("lydemapr", "rnaturalearthhires", "rnaturalearthdata", "dsmextra"))

renv::status()
# hydrate() corrected an issue where renv could not discover any local packages in the system library
renv::hydrate()

# install specific packages (usually needed for packages not on the CRAN)
devtools::install_github("densitymodelling/dsmextra") # dsmextra
devtools::install_github("ieco-lab/lydemapr") # lydemapr
devtools::install_github("ropensci/rnaturalearthhires")# rnaturalearthhires

# renv restore to previous package versions
# only use if the current package version need to be reverted to a previous state
renv::history()
renv::revert(project = "scari", commit = "")

# repair common issues
renv::repair()

# run this to rebuild and reinstall all packages in the library- this starts them from a clean slate
renv::rebuild()
# update packages
# only run as needed if a package breaks or something
renv::update()
# alternatively, install the latest CRAN versions:
renv::install()
# record newest package versions in lockfile
renv::snapshot()

remove renv

As needed, you may need to completely remove renv and restart from a fresh install.

# deactivate renv
renv::deactivate(project = file.path(here::here()))

# remove renv from project
renv::deactivate(project = file.path(here::here()), clean = TRUE)

# completely remove renv from the system
root <- renv::paths$root()
unlink(root, recursive = TRUE)
utils::remove.packages("renv")