JupyterNotebook R startup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cat('\n#########################################\n')
cat(paste0('#### Last run time: ', format(Sys.time(), '%Y-%m-%d %H:%M'), ' ####'))
cat('\n#########################################\n')
cat('\n#############################################################\n')
cat(paste0(R.home(), '\n'))
print(R.version)
cat('#############################################################\n\n')
cat('#############################################################\n')
sessionInfo()
cat('#############################################################\n\n')

JupyterNotebook R snippet for inline figure displaying size

1
2
3
4
5
6
7
base_len <- 6
#options(repr.plot.width = base_len, repr.plot.height = base_len)
#options(repr.function.highlight = TRUE)
mySetFigSize <- function(width = 1, height = 1) {
    options(repr.plot.width = base_len * width, repr.plot.height = base_len * height)
}
mySetFigSize(1, 1)

Setup s project directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
prj_home <- '~/Projects'
prj_dir <- 'ProjectName'
setwd(file.path(prj_home, prj_dir))
dir_in <- file.path(getwd(), 'raw_data')
dir_out <- file.path(getwd(), 'res_data')
dir_fig <- file.path(getwd(), 'output_plots')
dir_log <- file.path(getwd(), 'logs')
if(!(dir.exists(dir_in))) { dir.create(dir_in) }
if(!(dir.exists(dir_out))) { dir.create(dir_out) }
if(!(dir.exists(dir_fig))) { dir.create(dir_fig) }
if(!(dir.exists(dir_log))) { dir.create(dir_log) }

Batch loading packages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pkgs <- c(
    'ggplot2', 'RColorBrewer',
    'dplyr',
    'repr', 'highr'
)
 
invisible(lapply(pkgs,  \(.) { 
    suppressPackageStartupMessages(library(., character.only = TRUE)))
  }
)

A lightweight and sane ggplot2 theme

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
theme_barebone <- function(bssz = 18){
    base_size = bssz
    ggthemes::theme_foundation(base_size = bssz) +
    theme(
        plot.background = element_blank(),
        panel.background = element_blank(), 
        panel.border = element_blank(),
        panel.grid = element_blank(), 
        axis.line.x = element_line(colour = 'black', linewidth = 0.5, linetype = 'solid'), 
        axis.line.y = element_line(colour = 'black', linewidth = 0.5, linetype = 'solid'), 
        axis.ticks = element_line(colour = 'black', linewidth = 0.5, linetype = 'solid'),
        axis.ticks.length = unit(2, 'mm'),
        axis.text = element_text(colour = 'black', size = ceiling(base_size * 0.66), face = 'plain'), 
        axis.title = element_text(colour = 'black', size = ceiling(base_size * 0.75), face = 'plain'),
        strip.background = element_blank(), 
        strip.text = element_text(),
        strip.text.x = element_text(colour = 'black', vjust = 0.5, face = 'plain'), 
        strip.text.y = element_text(colour = 'black', angle = -90, face = 'plain'),
        legend.text = element_text(colour = 'black', size = ceiling(base_size * 0.66), face = 'plain'), 
        legend.title = element_text(colour = 'black', size = ceiling(base_size * .8), face = 'plain'),
        legend.position = 'right', 
        legend.key = element_blank(),
        legend.background = element_blank(), 
        legend.box.background = element_blank(),
        #plot.margin = unit(c(1, 1, 1, 0), 'mm'),
        plot.margin = margin(t = 5, r = 2, b = 2, l = 2), 
        plot.title = element_text(colour = 'black', size = ceiling(base_size), face = 'plain'),
        plot.subtitle = element_text(colour = 'black', size = ceiling(base_size * .8))
    )   
}

Pre-process data for heatmap visualization:

ChIP-seq data formats: BigWig, Wig and Bed files

R/Bioconductor: Genome and Annoation