Skip to content

Instantly share code, notes, and snippets.

@Dulani
Dulani / python_logging_examples.py
Created November 18, 2017 03:19
Example logging code for Python (since I'm always having to look it up).
#!/usr/bin/env python
"""
Originally taken from my pandoc filter code.
It has good examples of logging in use.
"""
import logging
LOG_FILENAME = 'my_log_name_here.log'
@Dulani
Dulani / xpath examples.txt
Created November 14, 2017 21:50
Some working Xpath expressions.
Xpath
#Starting with what you know you want an xpath expression for, pick it out and then pick its parent out.
//*[Name="Q1"]
//*[Name="Q1"]/parent::*
//Grid[Name="Q1"]/parent::*
#All the text attributes of the name elements of the grid nodes:
//Grid/Name/text()
@Dulani
Dulani / r_ubuntu_17_10.sh
Created October 22, 2017 14:23 — forked from pachadotdev/r_ubuntu_17_10.sh
Install R on Ubuntu 17.10
# Install R
sudo apt-get update
sudo apt-get install gdebi libxml2-dev libssl-dev libcurl4-openssl-dev r-base r-base-dev
# Install RStudio
cd Downloads
wget https://download1.rstudio.org/rstudio-xenial-1.1.383-amd64.deb
sudo gdebi rstudio-xenial-1.1.383-amd64.deb
printf '\nexport QT_STYLE_OVERRIDE=gtk\n' | sudo tee -a ~/.profile
@Dulani
Dulani / datatable.options.R
Created October 10, 2017 16:53
R DT datatable options
#Adjust the size of the scrollbar and the number of items on a "page" and also setting the height of the scroll bar.
library(tidyverse)
library(DT)
mtcars %>%
DT::datatable(., style ="default", options = list("scrollY" = "400px", "pageLength"= 120))
# An alternative style choice is style ="bootstrap"
@Dulani
Dulani / Rcpp.R
Created August 5, 2017 03:17
A VERY simple example using C++ in R with the cppFunction() function.
#' Copied from Dirk Eddelbuettel's 2017 Rcpp Tutorial
#' http://dirk.eddelbuettel.com/papers/useR2017_rcpp_tutorial.pdf
#A very impressive little adaptation:
cppFunction(”
double m(int n, double x) {
for (int i=0; i<n; i++)
x = 1 / (1+x);
return x;
}”
@Dulani
Dulani / pandoc.css
Created July 28, 2017 20:31 — forked from killercup/pandoc.css
Add this to your Pandoc HTML documents using `--css pandoc.css` to make them look more awesome. (Tested with Markdown and LaTeX.)
/*
* I add this to html files generated with pandoc.
*/
html {
font-size: 100%;
overflow-y: scroll;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
@Dulani
Dulani / pdfcrop.py
Created June 2, 2017 01:49 — forked from alexzhan/pdfcrop.py
Use pyPdf to crop a pdf file according to user inputs
#!/usr/bin/env python
import sys
if __name__ == '__main__' and len(sys.argv) > 5 and sys.argv[1][-3:].upper() == 'PDF':
original = sys.argv[1]
target = original[:-4] + '.cropped.pdf'
left = int(sys.argv[2])
top = int(sys.argv[3])
right = int(sys.argv[4])
@Dulani
Dulani / index.html
Created March 22, 2017 23:34 — forked from anonymous/index.html
NIJ PCJNI Delphi // source http://jsbin.com/micekex
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<script src="delphi-nij.js"> </script>
<link rel="stylesheet" type="text/css" href="delphi-nij.css">
<title>NIJ PCJNI Delphi</title>
@Dulani
Dulani / Desc_JSON_to_df.md
Created March 17, 2017 18:17 — forked from gluc/Desc_JSON_to_df.md
Convert a complex JSON to an R data.frame

This gist shows how to convert a nested JSON file to an R data.frame. To do this, it uses jsonlite and data.tree.

The gist contains two examples: one is a bit simpler, the second one a bit more advanced.

Example 1

In the first example, we download all the repos from Hadley Wickham's Github account from https://api.github.com/users/hadley/repos . This JSON contains a nested owner object. The code shows how to convert that in a flat data.frame in three statements:

  1. line 5: download
  2. line 8: convert to data.tree
@Dulani
Dulani / Multiply matrix columns by vector.Rmd
Last active February 15, 2017 20:34
Multiply the columns of the data frame by the elements of the vector in R
---
title: "Multiply the columns of the data frame by the elements of the vector"
output: html_notebook
---
Matrix:
```{r}
(mat <- matrix(c(4:9),ncol = 2))
```