Skip to content

Instantly share code, notes, and snippets.

@bhaskarvk
bhaskarvk / ipUtils
Last active August 29, 2015 14:16
Vectorized functions to convert ipv4 to/from integer
# Vectorized functions to convert ipv4 to/from integer
require(bitops) # required for unsigned int bitwise ops
int_to_ipv4 <- function(i) {
return(lapply(i,function(x) {
paste(bitAnd(bitShiftR(x,c(24,16,8,0)),0xFF),collapse='.')
}))
}
ipv4_to_int <- function(i) {
@bhaskarvk
bhaskarvk / .raliases
Created September 8, 2015 16:21
Bash functions to install R packages from github
rInstGithub() {
Rscript -e "devtools::install_github('$1')"
}
rInstBob() {
rInstGithub "hrbrmstr/$1"
}
rInstKent() {
rInstGithub "timelyportfolio/$1"
}
rInstHadley() {
@bhaskarvk
bhaskarvk / keybase.md
Last active October 23, 2015 16:12
keybase.md

Keybase proof

I hereby claim:

  • I am bhaskarvk on github.
  • I am bhaskarvk (https://keybase.io/bhaskarvk) on keybase.
  • I have a public key whose fingerprint is E92A 38BA A931 0D68 A2F5 4C30 B647 68F2 BF54 8FD9

To claim this, I am signing this object:

@bhaskarvk
bhaskarvk / solaris.diff
Last active October 25, 2015 01:45
Shiny Server patch for Solaris
diff --git a/src/launcher.cc b/src/launcher.cc
index d8d04b6..835b149 100644
--- a/src/launcher.cc
+++ b/src/launcher.cc
@@ -73,7 +73,11 @@ int main(int argc, char **argv) {
int findBaseDir(std::string* shinyServerPath) {
char execPath[MAXPATHLEN + 1];
+#ifdef __sun
+ int cn = snprintf(execPath, MAXPATHLEN + 1, "/proc/%d/path/a.out", getpid());
@bhaskarvk
bhaskarvk / purrr_walk_test.R
Last active March 6, 2016 17:37
Some code to test purrr::walk
# Sample df with missing values
df <- data.frame(col1=sample(c('','Y','N'),100,replace = TRUE),
col2=sample(c('', as.character(0:9)),100,replace = TRUE),
stringsAsFactors = FALSE)
cols <- colnames(df) # store colnames
lapply(df,table) # check levels
df.forFor <- df # to be fixed with a for loop
l1 <- list(as.list(1:4),
as.list(11:15))
l2 <- list(list('a.1'=1,'a.2'=2,'a.3'=3,'a.4'=4),
list('b.1'=1,'b.2'=2,'b.3'=3,'b.4'=4,'b.5'=5))
lapply(l1,names) # should be nulls
lapply(l2,names) # should be 'a.1' ...
# Use copy so as to leave l1 intact
t1 <- l1
@bhaskarvk
bhaskarvk / imager_vs_magick-read_perf.R
Last active August 24, 2016 00:58
Comparison of image read performance of imager and magick packages.
# You will need to generate images of varying size using ImageMagic's convert utility on the shell.
# The shell code below will not work under Windows. Sorry! bash-only.
# Run the following shell code to generate some random images
# mkdir -p ./data/RandomNoise
# (cd ./data/RandomNoise && for i in `seq 100 100 4000`; do convert -size ${i}x${i} xc: +noise Random random-${i}_$i.png;done)
# You'll need the following R packages
# dplyr, purrr, magick, imager, magrittr, gridExtra, ggplot2, stringr
d <- './data/RandomNoise'
We can make this file beautiful and searchable if this error is corrected: It looks like row 10 should actually have 8 columns, instead of 7. in line 9.
"reading_function","Iteration","File.Size","Object.Size","Mem.Increase","User.Time","System.Time","Elapsed.Time"
"magick::image_read",1,1576105,1049048,1051096,0.01400000000001,0.00200000000000955,0.0239999999994325
"magick::image_read",1,1994600,1327576,1329448,0.0209999999999582,0.00199999999999534,0.032999999999447
"magick::image_read",1,2462383,1638872,1640744,0.0240000000000009,0.00200000000000955,0.0399999999999636
"magick::image_read",1,2979319,1982936,1984808,0.0300000000000864,0.00300000000000011,0.0480000000006839
"magick::image_read",1,3545586,2359768,2361640,0.0359999999999445,0.00400000000000489,0.0560000000004948
"magick::image_read",1,4160985,2769368,2771240,0.0399999999999636,0.00400000000000489,0.0619999999998981
"magick::image_read",1,4825702,3211736,3213608,0.0489999999999782,0.00500000000000966,0.0750000000007276
"magick::image_read",1,5539565,3686872,3688744,0.0570000000000164,0.00600000000000023,0.0879999999997381
"magick::image_read",1,6302748,4194776,4196648,0.0490000000000919,0.001999
File.Size reading_function Object.Size Mem.Increase User.Time System.Time Elapsed.Time
1576105 magick::image_read 1049048 1050908.8 0.0163000000000125 0.00200000000000244 0.0262000000002445
1994600 magick::image_read 1327576 1329448 0.01899999999996 0.00289999999999822 0.0315000000001419
2462383 magick::image_read 1638872 1640744 0.0244000000000256 0.0032999999999987 0.0390000000003056
2979319 magick::image_read 1982936 1984808 0.0296000000000163 0.00450000000000017 0.047899999999936
3545586 magick::image_read 2359768 2361640 0.0365000000000009 0.0055000000000021 0.0559999999998581
4160985 magick::image_read 2769368 2771240 0.0395999999999844 0.00600000000000023 0.0625
4825702 magick::image_read 3211736 3213608 0.0467999999999847 0.00690000000000168 0.0738999999997759
5539565 magick::image_read 3686872 3688744 0.0539000000000101 0.00789999999999935 0.0832000000000335
6302748 magick::image_read 4194776 4196648 0.0580000000000268 0.0083999999999989 0.0868999999997868
File.Size reading_function Object.Size Mem.Increase User.Time System.Time Elapsed.Time
1576105 magick::image_read 1049048 1050920 0.0170000000000528 0.00200000000000244 0.0264999999999418
1994600 magick::image_read 1327576 1329448 0.0194999999999936 0.00300000000000011 0.0309999999999491
2462383 magick::image_read 1638872 1640744 0.0244999999999891 0.00300000000000011 0.0400000000004184
2979319 magick::image_read 1982936 1984808 0.0294999999999845 0.00499999999999545 0.0474999999996726
3545586 magick::image_read 2359768 2361640 0.0364999999999895 0.00600000000000023 0.0559999999995853
4160985 magick::image_read 2769368 2771240 0.0399999999999636 0.00600000000000023 0.0619999999998981
4825702 magick::image_read 3211736 3213608 0.0475000000000136 0.007000000000005 0.0750000000007276
5539565 magick::image_read 3686872 3688744 0.0534999999999854 0.00800000000000267 0.0825000000004366
6302748 magick::image_read 4194776 4196648 0.0579999999999927 0.00900000000000745 0.090999999999439