Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
johnmackintosh / interpolation2.R
Created October 20, 2021 22:25
no nonsense interpolation of integer as part of SQL SERVER dateadd function in R
ndays <- 7
ndays_sql <- ndays * -1
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "SERVER",
Database = "TABLE",
Trusted_Connection = "True",
Port = 1433)
@johnmackintosh
johnmackintosh / data.table-joins.R
Created March 2, 2021 23:27 — forked from nacnudus/data.table-joins.R
How to do joins with data.table
library(data.table)
?`[.data.table`
DT <- data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
X <- data.table(x=c("c","b"), v=8:7, foo=c(4,2))
colnames(DT)
# [1] "x" "y" "v"
@johnmackintosh
johnmackintosh / PBI.txt
Created January 31, 2021 20:22
Power BI resources
----
Power BI Official Software - Cost: $0 Dimes
----
Power BI Desktop. From the Microsoft Store updates automatically. #WINNING
URL: https://aka.ms/pbidesktopstore
Power BI Desktop. From download center requires manual updating. #NOTWINNING
library(lubridate)
library(dplyr)
library(tidyr)
library(readxl)
library(purrr)
library(here)
setwd(here("2021-04"))
wb <- "PD 2021 Wk 4 Input.xlsx"
library(lubridate)
library(dplyr)
library(tidyr)
library(readxl)
library(purrr)
library(here)
setwd(here("2021-03"))
library(here)
library(data.table)
library(stringr)
setwd(here("2021-02"))
# read in and process
DT <- fread('Input.csv')
library(data.table)
library(gsheet)
library(ggplot2)
link <- 'https://docs.google.com/spreadsheets/d/1GYv4573GnJa-C21NYeDj-OhFSTwrK0SnQNF2IQFqa50/edit#gid=0'
import <- gsheet2text(link, format = 'tsv')
DT <- fread(import)
DT[, Date := as.IDate(DT$Date, format = "%d/%m/%Y")]
@johnmackintosh
johnmackintosh / automate blog years.txt
Created January 1, 2021 12:29
Update the year in the copyright notice of blog post automatically
<script>document.write(new Date().getFullYear())</script>
or
<script>new Date().getFullYear()>2005&&document.write("-"+new Date().getFullYear());</script>
@johnmackintosh
johnmackintosh / link.txt
Created June 14, 2020 23:14
Great package/ OOP guide
@johnmackintosh
johnmackintosh / sqlFetchData.R
Created June 2, 2020 13:53
wrapper function for DBI /SQL Server that actually works - via https://stackoverflow.com/a/61073561/5175413
sqlFetchData <- function(connection, database, schema, table, nobs = 'All') {
#'wrap function to fetch data from SQL Server
#
#@ connection: an established odbc connection
#@ database: database name
#@ schema: a schema under the main database
#@ table: the name of the data table to be fetched.
#@ nobs: number of observation to be fetched. Either 'All' or an integer number.
# The default value is 'All'. It also supports the input of 'all', 'ALL' and