profandyfield / discovr

discovr package for R to accompany Discovering Statistics Using R and RStudio
132 stars 25 forks source link

discovr: Resources for Discovering Statistics Using R and RStudio (Field, 2023)

NOTE: This package is incomplete but under active development. Check back here for updates/new tutorials.

The discovr package contains resources for my 2023 textbook Discovering Statistics Using and . There are tutorials written using learnr. Once a tutorial is running it’s a bit like reading a book but with places where you can practice the code that you have just been taught. The discovr package is free and offered to support tutors and students using my textbook who want to learn .

Installing discovr

To use discovr you first need to install and and familiarise yourself with , and good workflow practice. You can do this using this interactive tutorial. Once you have installed and you can install discovr. The package is in development so you have to install it from github. To install the package execute (in ):

if(!require(remotes)){
  install.packages('remotes')
}

remotes::install_github("profandyfield/discovr")

If you are trying to install on a networked computer the install might fail (it’s to do install.packages not liking UNC paths, which I’m not even going to pretend to understand). The solution is to specify the location of your library at the point of install. Most networks will map network locations to a drive name (for example, at my own University, users accounts are on the ‘N’ drive). Find the location of your library (e.g., N:/Documents/R/win-library/3.5), possibly executing .libPaths() to help you, and specify this location using the lib argument:

remotes::install_github("profandyfield/discovr", lib = "N:/Documents/R/win-library/3.5")

Interactive tutorials

I recommend working through this playlist of tutorials on how to install, set up and work within and before starting the interactive tutorials.

List of tutorials

Running a tutorial

In Version 1.3 onwards there is a tutorial pane. Having executed

library(discovr)

A list of tutorials appears in this pane. Scroll through them and click on the button to run the tutorial:

Alternatively, to run a particular tutorial from the console execute:

library(discovr)
learnr::run_tutorial("name_of_tutorial", package = "discovr")

and replace “name of tutorial” with the name of the tutorial you want to run. For example, to run tutorial 2 execute:

learnr::run_tutorial("discovr_02", package = "discovr")

The name of each tutorial is in bold in the list above. Once the command to run the tutorial is executed it will spring to life in a web browser.

Suggested workflow

The tutorials are self-contained (you practice code in code boxes) so you don’t need to use at the same time. However, to get the most from them I would recommend that you create an project and within that open (and save) a new RMarkdown file each time to work through a tutorial. Within that Markdown file, replicate parts of the code from the tutorial (in code chunks) and use Markdown to write notes about what you have done, and to reflect on things that you have struggled with, or note useful tips to help you remember things. Basically, write a learning journal. This workflow has the advantage of not just teaching you the code that you need to do certain things, but also provides practice in using itself.

See this video explaining my suggested workflow:

Colour palettes

Inspired by the rockthemes package and adapting code form that package I have come up with a bunch of colour themes based around the studio albums of my favourite band Iron Maiden. Full disclosure, I’m not a designer, so this largely involved uploading images of their sleeves to colorpalettefromimage.com and seeing what happened. If you have a better palette design send me the hex codes for the colours! If you’re wondering why some albums are missing, here’s the explanation: X Factor (would basically be 8 shades of gray), Fear of the Dark (shit album), The Book of Souls (would basically be 8 shades of black).

There is also a colourblind-friendly colour pallette from

The following palettes exist.

To view the palette execute

scales::show_col(name_of_palette()(8))

Replacing name_of_palette() with the name, for example

scales::show_col(pom_pal()(8))

To apply, for example, the Powerslave palette to the colours of a ggplot2 plot add scale_color_power() as a layer:

library(ggplot2)

# Get albums in the classic era from the discovr::eddiefy data.
# I'm not including fear of the dark because it's not in any way classic.
# No prayer for the dying was pushing its luck too if I'm honest.

classic_era <- subset(discovr::eddiefy, year < 1992)
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

ggplot(classic_era, aes(x = energy, y = valence, color = album_name)) +
  geom_point(size = 2) +
  discovr::scale_color_power() +
  theme_minimal()

Similarly to apply the Powerslave palette to the fill of objects in a ggplot add scale_fill_power() as a layer:

ggplot(classic_era, aes(x = album_name, y = valence, fill = album_name)) +
  geom_violin() +
  discovr::scale_fill_power() + 
  theme(axis.text.x = element_text(angle = 45)) +
  theme_minimal()

Datasets

See the book or data descriptions for more details. This is a list of available datasets within the package. Raw CSV files are available from the book’s website.

Smart Alex solutions

Solutions for end of chapter tasks are available at www.discovr.rocks.

Labcoat Leni solutions

Solutions for the Labcoat Leni tasks are available at www.discovr.rocks.

Chapter code

Although I recommend working through the interactive solutions, each book Chapter has online code and a downloadable R Markdown file available from www.discovr.rocks.