Skip to content

Instantly share code, notes, and snippets.

View tomsing1's full-sized avatar

Thomas Sandmann tomsing1

View GitHub Profile
@tomsing1
tomsing1 / gist:cc684a3b8a5f09dcaa904012c7239fc3
Last active February 19, 2025 17:13
Comparison bars: Bar plot with shifted positions, facilitating comparisons (originally posted by https://bsky.app/profile/hrbrmstr.dev/post/3lik7thxdmc2m)
library(ggplot2)
library(hrbrthemes)
library(janitor)
read.csv(text='"Party","Votes in all of the U.K.","Votes only in Scotland"
"Labour",33.7,35.27801359
"Conservative",23.7,12.72746096
"Reform UK",14.3,6.956199453
"Liberal\nDemocrats",12.2,9.699645107
"Greens",6.7,3.838190168
@tomsing1
tomsing1 / plot_bad.R
Created February 10, 2025 17:54
Example of an R script that does not follow tidyverse style guide (for a tutorial using the {lintr} and {styler} R packages
library(ggplot2);data(mtcars)
do_sampling = T
if (do_sampling==T){
if(!is.logical(do_sampling)) stop()
my_cars<- mtcars[,sample(ncol(mtcars), repl = F)]
} else
{
my_cars = mtcars
@tomsing1
tomsing1 / exon_level_read_counting_with_featurecounts.sh
Created September 17, 2024 03:28
A script that retrieves BAM files from S3 and quantifies exon-level expression with featurecounts
#!/usr/bin/env bash
set -e
set -o nounset
# This script uses a GTF file (e.g. generated by Stringtie) and
# 1. retrieves BAM files for individual samples from AWS S3
# 2. quantifies the exons defined in the GTF file with featureCounts,
# e.g. for differential splicing analysis with limma::diffSplice
#
# Alignments are expected to be reverse-stranded and paired-end.
@tomsing1
tomsing1 / awswrangler_in_colab.py
Created August 21, 2024 19:54
Wrangling files on AWS S3 with the awswrangler python module (in Colab)
# Commented out IPython magic to ensure Python compatibility.
# %%capture
# !pip install awswrangler --quiet
import awswrangler as wr
import pandas as pd
import boto3
from google.colab import data_table, userdata
data_table.enable_dataframe_formatter()
@tomsing1
tomsing1 / boto3_in_colab.py
Created August 21, 2024 19:41
Accessing AWS within a Google Colab using the built-in secret manager
import boto3
from google.colab import userdata
# refresh the temporary credentials via the key symbol in the left hand tab
s3_resource = boto3.resource('s3',
aws_access_key_id=userdata.get('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=userdata.get('AWS_SECRET_ACCESS_KEY'),
aws_session_token=userdata.get('AWS_SESSION_TOKEN')
)
@tomsing1
tomsing1 / glue_sql_quoting.R
Created April 19, 2024 17:33
Controlling quoting by the glue::glue_sql() function
library(glue)
library(RSQLite)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
var = "test"
glue_sql("{var}", .con = con) # <SQL> 'test'
glue_sql("{`var`}", .con = con) # <SQL> `test`
glue_sql("{DBI::SQL(var)}", .con = con) # <SQL> test (unquoted)
glue_sql("`{var}`", .con = con) # <SQL> `'test'` NOT USEFUL
@tomsing1
tomsing1 / conditionalPanel_usage_in_module.R
Last active April 7, 2024 19:33
Modularized shiny app using conditional panels (modified from the shiny::conditionalPanel help page)
# The following code is based on the shiny::conditionalPanel() help page. The `ui` and `server`
# components have been compartmentalized into the `mod_histogram` module. The `histogramApp`
# function shows an example of using this module.
# Note that the `ns = ns` argument needs to be passed ot the conditionalPanel() call.
library(shiny)
mod_histogram_ui <- function(id){
ns <- NS(id)
fluidPage(
@tomsing1
tomsing1 / biosample_attributes.R
Created January 25, 2024 17:22
Retrieve sample attributes from EBI's ENA repository in JSON format
library(glue)
library(httr)
library(jsonlite)
library(xml2)
#' Query ENA's REST API for information about records
#'
#' @param accessions Character vector of one or more ENA record identifiers
#' @return An `xml_document` object
get_records <- function(accessions) {
@tomsing1
tomsing1 / order_input.R
Created January 16, 2024 04:54
Reordering the levels of categorical variables in a shiny app with shinyjqui
library(palmerpenguins)
library(shiny)
library(shinyjqui)
categories <- colnames(penguins)[vapply(penguins, is.factor, logical(1))]
server <- function(input, output) {
lapply(categories, \(category) {
output[[category]] <- renderPrint({ print(input[[category]]) })
})
@tomsing1
tomsing1 / image_placeholder.R
Created January 8, 2024 19:09
Create an image tag pointing to a random picture from Lorem Picsum
#' Create an image tag with an example image
#'
#' @param width Scalar integer, the width of the image
#' @param height Scalar integer, the height of the image
#' @param title Scalar character, the title of the image
#' @return A `shiny.tag` with the URL to a random image from
#' [Lorem Picsum](https://picsum.photos/)
#' @export
#' @importFrom htmltool tags
#' @importFrom checkmate assert_count assert_character