class: center, middle, inverse, title-slide # Getting to know your R Setup ## ⚔
and other demystifications ### Julia Piaskowski ### 2022-03-03 --- ## Updating R <small>*(please don't do this in the middle of the workshop)*</small> **Windows** ``` library(installr); updateR() # follow prompts ``` **Mac** Visit the [website](https://cran.r-project.org/bin/macosx/), download and install. **Linux** Run in terminal: ``` sudo apt update sudo apt upgrade ``` --- ## Updating packages * Command line - single package: ```r devtools::update.packages("RColorBrewer") ``` * Command line - all packages: <small>*(don't run this unless you have the time to wait)*</small> ```r devtools::update_packages(TRUE) ``` * point-and-click using RStudio's "Packages" Pane --- ## Downgrade a package Easiest way (if you know the version): ```r devtools::install_version("RColorBrewer", "1.0.5") ``` Downgrade to a specific point in time: *(note that going this far back in time usually causes problems)* ```r groundhog::groundhog.library("RColorBrewer", "2010-01-01") ``` How to find the version information? Visit the [package site](http://localhost:4321/2022-03-01-WhatTheyForgot) on [CRAN](https://cran.r-project.org/web/packages/available_packages_by_name.html). --- ## Installing packages when you update R * If doing a minor update of R (e.g. 4.0.1 to 4.0.3), do nothing (packages will remain compatible) * If doing a major R update (4.0 to 4.1), re-install packages. *Don't copy over packages directly because things may break without telling you.* One possible [solution](https://rstats.wtf/maintaining-r.html): ```r # Install new version of R (4.1 in this example) Create a # new directory for the version of R fs::dir_create("~/Library/R/4.1/library") # Re-start R so the .libPaths are updated OR find where # your R library exists already with .libPaths() Look up # what packages were in your old package library pkgs <- fs::path_file(fs::dir_ls("~/Library/R/3.6/library")) # Filter these packages as needed Install the packages in # the new version install.packages(pkgs) ``` --- ## Where does R live? <img src="images/where_is_waldo.jpeg" width="60%" /> --- ## Where the R executable(s) live(s) This tell us the path to R that RStudio is using: ```r R.home() ``` ``` ## [1] "/usr/lib/R" ``` It can sometimes be more [complicated](https://support.rstudio.com/hc/en-us/articles/200486138-Changing-R-versions-for-the-RStudio-Desktop-IDE) than that depending on the system. --- ## Where your package libraries live ```r .libPaths() ``` ``` ## [1] "/home/jpiaskowski/R/x86_64-pc-linux-gnu-library/4.1" ## [2] "/usr/local/lib/R/site-library" ## [3] "/usr/lib/R/site-library" ## [4] "/usr/lib/R/library" ``` 1. main library 1. empty 1. added via the command line and updates with `sudo apt-get update` 1. "base R packages" (what is shipped with R) R is using all of these, in order. --- ## Check your default library (by installing a package) ```r install.packages("RColorBrewer") ``` ``` Installing package into ‘/home/jpiaskowski/R/x86_64-pc-linux-gnu-library/4.1’ (as ‘lib’ is unspecified) ``` --- class: middle, center, inverse # terminal fun ## [exercise](exercises/exercise-1.html) --- background-image: url(https://raw.githubusercontent.com/imci-idaho/2022-03-01-WhatTheyForgot/main/static/slides/images/R-startup.svg) background-position: center background-size: contain class: bottom, left ## What Happens When [R is Started?](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html) --- # .Renviron * Sets global variables: API and authentication keys * There is often only one of these per R system, located in the home directory, but there can one per project * DO NOT add this to a git repository or make sure it ends up in *.gitignore* -- Example: ```r if (!file.exists(".Renviron")) file.create(".Renviron") ``` Open file and manually add: ```r NOAA_KEY = "my-secret-key" ``` Some helper functions from **usethis**: ```r usethis::edit_r_environ("user") # system-wide usethis::edit_r_environ("project") # for the current project you are in ``` --- # .Rprofile * An <mark>R script</mark> that runs every time R is opened or restarted * there is (often) one created automatically when R is installed at the home directory * There may be one in each **.Rproj**, this takes precedence over others * This often goes in the git repo --- ## A possible .Rprofile ``` options(scipen=999) # force R to not use scientific notation .First <- function() { cat("Welcome, High Priestess of R. You are a true hero and your legend will never die.") } library(tidyverse) # probably not a good idea source("scripts/file_import.R") .Last <- function() { cat('Praised be, my R programmer.') } ``` more [details](https://sodocumentation.net/r/topic/4166/-rprofile) and [options](https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html) --- class: center, middle, inverse .pull-left[ <img src="images/prince.jpg" width="200%" /> ] .pull-right[ [exercise](exercises/exercise-2.html) ] --- # Namespace conflicts ```r library(dplyr) ``` ``` ## ## Attaching package: 'dplyr' ``` ``` ## The following objects are masked from 'package:stats': ## ## filter, lag ``` ``` ## The following objects are masked from 'package:base': ## ## intersect, setdiff, setequal, union ``` Only one function name can be loaded into an R session. Identical function names for packages loaded earlier must be referenced like thus: `base::filter()`. --- # Find conflicts: ```r getAnywhere("filter") ``` ``` ## 2 differing objects matching 'filter' were found ## in the following places ## package:dplyr ## package:stats ## namespace:dplyr ## namespace:stats ## Use [] to view one of them ``` --- ## Namespace solutions * Use `::` ```r dplyr::filter(...) ``` * Remove conflicting packages: ```r unloadNamespace("offending_package") ``` * Only require packages you will use (reconsider `library(tidyverse)`) * Load the packages with whose functions you do not want masked last * Use the package **conflicted** --- ## A namespace solution under development: ```r # devtools::install_github('r-lib/conflicted') library(conflicted) library(dplyr) conflict_scout() ``` ``` ## 2 conflicts: ## * `filter`: dplyr, stats ## * `lag` : dplyr, stats ``` ```r conflict_prefer("filter", "dplyr") ``` ``` ## [conflicted] Will prefer dplyr::filter over any other package ``` --- # Find all the loaded namespaces: ```r sort(loadedNamespaces()) ``` ``` ## [1] "assertthat" "base" "bslib" "cachem" "cli" ## [6] "compiler" "conflicted" "crayon" "datasets" "DBI" ## [11] "digest" "dplyr" "ellipsis" "evaluate" "fansi" ## [16] "fastmap" "formatR" "generics" "glue" "graphics" ## [21] "grDevices" "highr" "htmltools" "jquerylib" "jsonlite" ## [26] "knitr" "lifecycle" "magrittr" "memoise" "methods" ## [31] "pillar" "pkgconfig" "purrr" "R6" "rlang" ## [36] "rmarkdown" "rstudioapi" "sass" "stats" "stringi" ## [41] "stringr" "tibble" "tidyselect" "tools" "utf8" ## [46] "utils" "vctrs" "xaringan" "xfun" "yaml" ```