Skip to contents

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 followin 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.

Setup

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
##  dplyr     1.1.4      readr     2.1.5
##  forcats   1.0.0      stringr   1.5.1
##  ggplot2   3.5.2      tibble    3.3.0
##  lubridate 1.9.4      tidyr     1.3.1
##  purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
##  dplyr::filter() masks stats::filter()
##  dplyr::lag()    masks stats::lag()
##  Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'renv'
## 
## The following object is masked from 'package:purrr':
## 
##     modify
## 
## The following objects are masked from 'package:stats':
## 
##     embed, update
## 
## The following objects are masked from 'package:utils':
## 
##     history, upgrade
## 
## The following objects are masked from 'package:base':
## 
##     autoload, load, remove, use
## here() starts at C:/Users/tun83449/OneDrive - Temple University/Shared drives/slfClimate/projects/scari/scari
## Loading required package: usethis
## 
## Attaching package: 'devtools'
## 
## The following object is masked from 'package:renv':
## 
##     install

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"))

Get 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()
## Finding R package dependencies ... Done!
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
##  Please use `reframe()` instead.
##  When switching from `summarise()` to `reframe()`, remember that `reframe()`
##   always returns an ungrouped data frame and adjust accordingly.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

one-time renv initialization

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 

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

# check status
renv::status()

# you may need to call this if the dependencies are used and installed, but not recorded
renv::snapshot()

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 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)

Troubleshooting

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)
# 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()
# now install 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()

# update renv to latest version in this project (but this will differ from the source download of this project folder)
renv::upgrade()

# 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")