Skip to content

Instantly share code, notes, and snippets.

View tomsing1's full-sized avatar

Thomas Sandmann tomsing1

View GitHub Profile
@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
@tomsing1
tomsing1 / pyenv.md
Last active December 28, 2023 00:16
Using pyenv to manage multiple python versions & virtual environments

There is a lot of confusing information about virtual environments in python out there, in part because the tool chain has evolved over many years.

I decided to follow the advice of Real python and The hitchhiker's guide to python and manage both multiple python versions and multiple virtual environments with pyenv

@tomsing1
tomsing1 / quarto_webr_example.qmd
Created November 19, 2023 17:32
Quarto markdown file that uses the quarto-webr extension to run R code in the browser
---
title: "Embedding R into Quarto documents with quarto-webr"
subtitle: "Example: intersecting differential expression results"
author: "Thomas Sandmann"
date: '2023/11/18'
format: html
engine: knitr
webr:
show-startup-message: true
packages: ['ggvenn', 'huxtable']
@tomsing1
tomsing1 / fgsea.md
Created September 29, 2023 21:15
fgsea output

The fgsea::fgsea function returns a data.frame with the following columns:

  • pathway – name of the pathway as in ‘names(pathway)‘;
  • pval – an enrichment p-value;
  • padj – a BH-adjusted p-value;
  • ES – enrichment score

    GSEA calculates the ES by walking down the ranked list of genes, increasing a running-sum statistic when a gene is in the gene set and decreasing it when it is not. The magnitude of the increment depends on the correlation of the gene with the phenotype. The ES is the maximum deviation from zero encountered in walking the list.

@tomsing1
tomsing1 / postgres_json_duckdb.R
Last active September 10, 2023 18:07
duckdb: Retrieving a Postgres table with a json column and unpacking it
library(duckdb)
library(jsonlite)
library(purrr)
con <- dbConnect(duckdb())
# duckdb: install and load postgres extension to connect
# to a Postgres database via duckdb
dbExecute(con, "INSTALL postgres;")
dbExecute(con, "LOAD postgres;")
@tomsing1
tomsing1 / gorilla.csv
Created August 28, 2023 17:06
CSV file with simluated steps & bmi data, similar to Yanai and Lercher, biorXiv, 2020
bmi steps group
29.96 145.631067961165 F
29.8981818181818 10048.5436893204 M
23.4690909090909 3859.22330097087 M
26.0345454545455 7718.44660194175 M
19.5127272727273 10776.6990291262 M
29.6509090909091 3932.03883495146 M
30.3 2730.58252427184 M
25.1381818181818 7245.14563106796 F
29.4654545454545 11504.854368932 M