Skip to content

Instantly share code, notes, and snippets.

@micstr
micstr / checkDFnamesidentical.R
Created May 20, 2020 12:36
Check names in two tables are the same (say pre merge)
# identical check - Thanks Akrun
#https://stackoverflow.com/q/26566251/4606130
#RIGHT
identical(sort(names(data.store)), sort(names(data.input))))
# TRUE
#WRONG
# I had below which is flummoxed by order
@micstr
micstr / EOMONTH.R
Created May 15, 2020 12:23
Find out day of month EOMONTH in Excel
library(lubridate)
lubridate::ceiling_date(input$leaddaterange[1], "month") - 1
@micstr
micstr / dateRangeShowMonthlyOnly.R
Created May 15, 2020 05:27
Shiny inptDateRange setting min view to only show months for example not days
# Thanks to David https://stackoverflow.com/a/54922170/4606130
# date range input set to month with minview
# https://stackoverflow.com/a/54922170/4606130
dateInput2 <- function(inputId, label, minview = "days", maxview = "decades", ...) {
d <- shiny::dateInput(inputId, label, ...)
d$children[[2L]]$attribs[["data-date-min-view-mode"]] <- minview
d$children[[2L]]$attribs[["data-date-max-view-mode"]] <- maxview
# Logging use of tictoc notes
# See https://stackoverflow.com/a/47954069/4606130
# https://www.jumpingrivers.com/blog/timing-in-r/#the-tictoc-package
tic("timer")
1+1
# When log = TRUE, toc() pushes the measured timing to a list
# quiet = TRUE prevents from printing the timing
@micstr
micstr / test-chartlabels-mcve-app.R
Created July 25, 2017 09:35
missing chart labels MCVE example
# Test app chartlabels missing fixes
# (mcve Smaller subset version with no source)
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(googleVis)) # else get startup msg
library(shiny)
# FUNCTIONS
# Simplifying output
drawGraphAndTable <- function(title, name)
@micstr
micstr / fixTZinShinyTables.R
Created July 4, 2017 14:19
Fix TZ showing in Shiny tables
# Fix TZ showing in tables : drawdown Shiny example
# micstr 4 July 2017
# shiny 0.12. broke the perfapp drawdown tables now showing dates with Timezone e.g.
# 2016-04-30T00:00:00Z
#"POSIXt objects are now serialized to JSON in UTC8601 format (like
#"2015-03-20T20:00:00Z"), instead of as seconds from the epoch. If you
#have a Shiny app which uses sendCustomMessage() to send datetime
#(POSIXt) objects, then you may need to modify your Javascript code to
#receive time data in this format."
@micstr
micstr / droprowdatatable.R
Last active August 29, 2015 14:18
Drop row in data table
library(data.table)
dt <- data.table(name = c("M","P","S"),
pet = c("dog","cat","fish"))
# famous question can be simple
# http://stackoverflow.com/questions/12328056/how-do-i-delete-rows-in-a-data-frame
dt <- dt[-c(1:2),]
# Note it is called subsetting
@micstr
micstr / rbindnewcols.R
Created March 30, 2015 14:07
Bind new rows to a log if columns added in R
# Test rbind
# Kudos to http://stackoverflow.com/questions/15014339/rbind-with-new-columns-and-data-table
# old log
dt.1 <- data.table(message = c("A-ok", "no", "yes"),
date = c("Feb", "Mar", "Mar"))
# new log more columns
dt.2 <- data.table(message = c("watch"),
rows.added = c(100),
@micstr
micstr / comparedatatable.R
Created March 27, 2015 13:48
Compare Data Tables in R
# Comparing data tables examples
# Examples from
# http://cran.r-project.org/web/packages/data.table/data.table.pdf
# page 14
library(data.table)
dt1 <- data.table(A = letters[1:10], X = 1:10, key = "A") # a to j
dt2 <- data.table(A = letters[5:14], Y = 1:10, key = "A") # e to n
identical(all.equal(dt1, dt1), TRUE)
@micstr
micstr / identifymachine.R
Created March 20, 2015 08:16
identify machine name to adapt server connections or directories
# Identify what machine you are running on in R
# Use [[ to get the name out of entry
# Help from http://r.789695.n4.nabble.com/computer-name-td3593120.html
k.machine.name <- Sys.info()[["nodename"]]
k.dir.proj = "C:/Users/Michael/GitHub/FVCA" #ALIEN
if (identical(k.machine.name, "FVCA-BLOOMBERG")) {
k.dir.proj <- "C:/Users/Info/Documents/GitHub/FVCA" #FVCA-BLOOMBERG OVERRIDE
}