Replicability in is vital for science, but we may get it every time.
Reproducibility ensures that using the obtained data anyone can achieve the same results.This is not as easy as it seems because it necessitates (1) methodological procedures and analytic pipeline to be explicitly documented in their entirety (like a recipe), and (2) the use of a fully reproducible environment.
To give you an example, here we will reproduce the hexagonal logo
(sometimes refereed to as hex-stickers)
of our course. Keep in mind that this example is only partial
computational reproducibility, as we employ only package management. R
version, system packages, operating system can vary and bias our
results.
Full computational reproducibility, achievable with Docker containers or NixOS derivations, is beyond the scope of this demo.
For simplicity, we just store package versions within the .Rmd file
and use renv for package management and taking care of all
other dependencies.
Take note that renv is more powerful then this and
should typically be used in a project-centric (i.e. .prj)
workflow.
if (!require("renv")) install.packages("renv", dependencies = TRUE)
renv::use(
"renv@1.0.3",
"here@1.0.1",
"dplyr@1.1.4",
"tidyr@1.3.0",
"ggseg3d@1.6.3",
"ggseg@1.6.5",
"plotly@4.10.3",
"RColorBrewer@1.1-3",
"reticulate@1.34.0",
"magick@2.8.2",
"cropcircles@0.2.4",
"hexSticker@0.4.9"
)
# Packages
library(renv)
library(here)
library(dplyr)
library(tidyr)
library(ggseg)
library(ggseg3d)
library(plotly)
library(RColorBrewer)
library(reticulate)
library(magick)
library(cropcircles)
library(hexSticker)
As we are doing everything within a single .Rmd file. Here we use an
unusual way of setting up an working directory for the .Rmd file that
can be reproduced on any other system. Nevertheless, the usual way would
be to use knitr options to set a root directory.
# Settings
# If working in .prj directory and not using RStudio
here::set_here()
## File .here already exists in C:\Users\User\c\Desktop\R projects and stuff\brain_hex\brain_hex
unloadNamespace("here") # need new R session or unload namespace for .here file to take precedence over .Rproj
script_path <- file.path(here::here())
# If using RStudio
# script_path <- dirname(rstudioapi::getActiveDocumentContext()$path)
# Create output folder if it doesn't exist
setwd(script_path)
folder_name <- "output"
if(!exists(folder_name)) {dir.create(folder_name)}
## Warning in dir.create(folder_name): 'output' already exists
setwd(file.path(script_path, folder_name))
The package aseg_3d helps us build a plotly
interactive plot using a brain atlas.
# Make the Brain plot -------------------------------------------------------------------------
scene <- list(eye = list(x = -1.5, y = 0.3, z = 0))
my_aseg <- aseg_3d %>%
unnest(cols = ggseg_3d) %>%
select(label, colour) %>%
mutate(coln = as.integer(as.factor(colour))) %>%
mutate(coln = if_else(grepl("Cerebellum|Ventricle|Vent", label), NA_integer_, coln)) %>%
mutate(coln = if_else(grepl("CC_", label), 1L, coln)) %>%
mutate(col = RColorBrewer::brewer.pal(11, "Paired")[12 - as.integer(as.factor(coln))]) %>%
mutate(col = if_else(grepl("Cerebellum", label), "#012169", col)) # UB color
brain_plotly <-
ggseg3d(.data = my_aseg, atlas = aseg_3d,
colour = "col",
na.colour = "#A6CEE3",
show.legend = TRUE
) %>%
add_glassbrain(
hemisphere = "left",
colour = "#A6CEE3", # "#cecece"
opacity = 0.3
) %>%
remove_axes() %>%
pan_camera(camera = scene) %>%
hide_colorbar()
You can interact with this visualization of a glass brain showing its subcortical structures.
brain_plotly