Skip to content

Instantly share code, notes, and snippets.

View bohdanszymanik's full-sized avatar

Bohdan Szymanik bohdanszymanik

  • Wellington, New Zealand
View GitHub Profile
@bohdanszymanik
bohdanszymanik / example.R
Created November 6, 2023 00:28
From a tibble map a (slow) function over a column to calculate a new column with a progress bar
library(tidyverse)
1:1000 |>
as_tibble_col(column_name="some_column") |>
select(some_column) |>
mutate(some_other_column = map(some_column, function(x){Sys.sleep(1/100);(1/x)}, .progress = list(
type = "iterator",
format = "Calculating {cli::pb_bar} {cli::pb_percent}",
clear = TRUE)))
@bohdanszymanik
bohdanszymanik / db2se_create_srs.txt
Last active September 22, 2023 00:42
Syntax to create NZTM
db2se create_srs testdb -srsName NZTM -srsID 2193 -xScale 500 -yScale 500 -xMin 900000 -xMax 1.8014399E13 -yMin 4000000 -yMax 1.8014403E13 -zMin 0 -zMax 9.0071993E15 -mMin 0 -mMax 9.0071993E15 -coordsysName \"NZGD_2000_NEW_ZEALAND_TRANSVERSE_MERCATOR\" -description 'SRS NZTM'
@bohdanszymanik
bohdanszymanik / typescriptDb2.ts
Last active August 29, 2023 01:11
Typescript querying DB2
// Assumes you've installed
// * the free IBM docker db2 edition
// * ibm_db for node: https://github.com/ibmdb/node-ibm_db
// Brings back multiple records as well. Found on the net can't remember where - oh, lots of js docs here: https://github.com/ibmdb/node-ibm_db/blob/master/APIDocumentation.md
import * as db2 from 'ibm_db';
let cn: string = "DRIVER={DB2};DATABASE=testdb;HOSTNAME=127.0.0.1;UID=db2inst1;PWD=password;PORT=50000;PROTOCOL=TCPIP";
db2.open(cn, function(err: any, conn: any){
if (err) return console.log(err);
conn.query('select 1 from sysibm.sysdummy1', function (err: any, data: any) {
remove.packages("rlang")
remove.packages("tibble")
rlangUrl <- "https://cran.r-project.org/bin/windows/contrib/4.0/rlang_1.0.2.zip"
install.packages(rlangUrl, repos=NULL, type="binary")
tcltkUrl <- "https://cran.r-project.org/bin/windows/contrib/4.0/tcltk2_1.2-11.zip"
install.packages(tcltkUrl, repos=NULL, type="binary")
knitrUrl <- "https://cran.r-project.org/bin/windows/contrib/4.0/knitr_1.38.zip"
tcltkUrl <- "https://cran.r-project.org/bin/windows/contrib/4.0/tcltk2_1.2-11.zip"
install.packages(tcltkUrl, repos=NULL, type="binary")
# tkplotUrl <- "https://cran.r-project.org/bin/windows/contrib/4.0/tcltk2_1.2-11.zip"
# install.packages(tkplotUrl, repos=NULL, type="binary")
# library(tkplot)
knitrUrl <- "https://cran.r-project.org/bin/windows/contrib/4.0/knitr_1.38.zip"
install.packages(knitrUrl, repos=NULL, type="binary")
@bohdanszymanik
bohdanszymanik / countInAreas.R
Last active July 19, 2021 22:50
R sample to count locations in areas
##################################################################################
# Import school locations
# Import some areas
# Join counts
# write out
library(readxl)
library(writexl)
library(sf)
@bohdanszymanik
bohdanszymanik / mappingWithGgplot.R
Created May 24, 2021 02:35
Using ggplot to display a map plot with an underlying tiles
library(tidyverse)
library(readr)
library(ggmap)
library(ggplot2)
library(readxl)
library(plotly)
library(here)
library(httr)
@bohdanszymanik
bohdanszymanik / dateChange.R
Created May 24, 2021 02:30
Random date formatting example in R so I don't forget how it's done
library(readr)
library(stringr)
library(stringi)
em <- read_csv("C:/somewhere/some.csv")
em$Time <- paste(
paste(str_sub(em$`Date & Time`, 7, 10), str_sub(em$`Date & Time`, 4, 5), str_sub(em$`Date & Time`, 1, 2), sep="/"),
str_sub(em$`Date & Time`, -8), sep = " "
)
@bohdanszymanik
bohdanszymanik / st_join_example.R
Created May 19, 2021 20:46
Expanded example of joining spatial datasets, counting intersecting points across shapes, and using as an aesthetic
# refer to https://gis.stackexchange.com/questions/110117/counting-number-of-points-in-polygon-using-r/270479#270479
# for original code example
# Load libraries ----------------------------------------------------------
library(raster)
library(sf)
library(dplyr)
# Get sample data ---------------------------------------------------------
@bohdanszymanik
bohdanszymanik / LiquorLicenceRegisterAddressLookup.R
Last active May 7, 2021 04:04
nominatum openstreetmap address lookup for the liquor licencing register
##################################################################################
# Import liquor licencing data, geocode and plot
# Plot on a NZ map
# Show a regional view - some demographics eg population density at SA2
# Import the public NZ Police aggregated crime data
# Look at crime harm regional cf liquor outlet distribution
#
library(readxl)
library(rgdal)