Skip to content

Instantly share code, notes, and snippets.

@ChrKoenig
ChrKoenig / example_app.R
Created June 19, 2022 19:34
Insert/Remove UI elements in R Shiny
library(shiny)
ui = fluidPage(
titlePanel("My favorite movies"),
hr(),
# ------------------------------------- #
# Input form
tags$h3("Input form"),
fluidRow(
@ChrKoenig
ChrKoenig / PySnake.py
Last active September 9, 2021 21:26
PySnake - Snake clone using tkinter
"""
A Simple Snake clone using tkinter
Author: Christian König
Date created: 09.09.2021
Python Version: 3.8.6
"""
import tkinter as tk
import random
@ChrKoenig
ChrKoenig / thin_coordinates.R
Last active May 12, 2021 11:29
Function for fast spatial thinning of geographic coordinates
thin_coordinates = function(coords, threshold){
# Function for faster spatial thinning of coordinates
# coords: geographic coordinates in lon/lat format
# threshold: minimum distance between points in meters
coords_dist = geosphere::distm(coords, fun = distHaversine) # Great circle distance between points
coords_dist[upper.tri(coords_dist, diag = T)] = NA # Triangular distance matrix
coords_flagged = which(coords_dist < threshold, arr.ind=TRUE) # Two-column matrix of coordinate-pairs that are below threshold
pts_remove = c()
while(nrow(coords_flagged) > 0){
@ChrKoenig
ChrKoenig / TRY_dispersal_recoding.csv
Created December 2, 2020 15:39
Simplification and cleaning for TRY dispersal traits (TraitID: 28)
OrigValueStr dispersal_syndrome n
'Probably' Pongo pygmaeus NA 1
(water) hydrochorous 9
(water?) hydrochorous 1
0 unspecialized 7172
0.2 NA 5
0.25 NA 6
0.333333333 NA 19
0.5 NA 31
0.6 NA 2
@ChrKoenig
ChrKoenig / TRY_pollination_recoding.csv
Created December 2, 2020 15:34
Simplification and cleaning for TRY pollination traits (TraitID: 29)
OrigValueStr pollination_syndrome n
1 abiotic 11
2 abiotic 16
3 biotic 4
abio abiotic 39
Anemofila abiotic 4
anemogamous abiotic 246
anemogamous/entomogamous abiotic 35
Animals biotic 58
Animals Wind NA 3
graph_to_newick = function(graph, root){
##### Define callback functions for DFS
# 1. function to be called whenever a vertex is visited
f.in <- function(graph, data, extra) {
curr_node = extra$names[data['vid']+1] # Get vertex name (Add 1 to index because igraph uses 0-based indexing)
prev_node = extra$order.in[which(extra$order.in == curr_node)-1]
next_node = extra$order.out[which(extra$order.out == curr_node)+1]
if(length(extra$distances[prev_node]) == 0){ # first node / root
cat("")
} else{