Skip to content

Instantly share code, notes, and snippets.

View kaz-yos's full-sized avatar
💭
I may be slow to respond.

Kazuki Yoshida kaz-yos

💭
I may be slow to respond.
View GitHub Profile
@kaz-yos
kaz-yos / tableone_helper_functions.R
Last active April 12, 2018 20:54
tableone xlsx export helper functions using openxlsx
### tableone export helpers
### Turn tableone output matrix into tidyverse data_frame
tableone_mat_to_data_frame <- function(mat) {
mat %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "Variable") %>%
tibble::as_data_frame()
}
@kaz-yos
kaz-yos / mathematically-sane-org-table-create.el
Last active May 24, 2018 15:02
Mathematically sane org-table-create by advising it
;; Swap dimension specification
(defun my-reverse-org-table-dimension (size)
(let* ((split (org-split-string size " *x *"))
(pos1 (nth 1 split))
(pos0 (car split)))
(concat pos1 "x" pos0)))
;; Wrapper to fix the dimension
(defun org-table-create-reverse-table-dimension (old-fun &optional size)
"Query for a size and insert a table skeleton.
SIZE is a string Rows x Columns like for example \"2x3\".
@kaz-yos
kaz-yos / org-mode-ipython-issue.el
Created November 14, 2017 03:07
Offending functions in "Evaluation of this ipython code block is disabled."
;;; Issue reported by Brian
;; An ipython code snippet does not execute upon C-c C-c in org-mode.
;; "Evaluation of this ipython code block is disabled." is encountered.
;;; ob-core.el
;; This is the function showing the error message.
(defun org-babel-check-evaluate (info)
"Check if code block INFO should be evaluated.
Do not query the user, but do display an informative message if
evaluation is blocked. Returns non-nil if evaluation is not blocked."
@kaz-yos
kaz-yos / tsv_to_json.R
Created November 14, 2017 02:53
tsv to json in R (org-mode)
#+BEGIN_SRC R :session *R* :results output :exports both
suppressPackageStartupMessages(library(tidyverse))
data1 <- read_tsv("./data.tsv")
print(data1,
width = Inf)
library(jsonlite)
jsonlite::toJSON(data1, pretty = TRUE)
#+END_SRC
@kaz-yos
kaz-yos / write_lst_of_df_to_xls.R
Created October 15, 2017 16:12
Write multiple data_frames to a single xlsx file (use openxlsx)
### Write multiple data_frames to a single xlsx file (use openxlsx)
write_lst_of_df_to_xlsx <- function(lst_df, file, font_size) {
## Create a workbook object with one sheet
## https://rdrr.io/cran/openxlsx/man/setColWidths.html
wb <- createWorkbook()
## Work on each data_frame
for (i in seq_along(lst_df)) {
sheet_name <- ifelse(is.null(names(lst_df[i])),
@kaz-yos
kaz-yos / base_r_to_tidyverse.txt
Created October 12, 2017 15:51
Switching from Base R to tidyverse
Taken from http://www.significantdigits.org/2017/10/switching-from-base-r-to-tidyverse/
| Base R command | Tidyverse Command | Comment |
|-----------------------------------+--------------------------------------------+---------------------------------------------------------------|
| read.csv() | read_csv() | also see read_delim(), read_tsv() and readxl::read_xlsx() |
| sort(), order() | arrange() | see also order_by() |
| mtcars$mpg = ... | mutate() | see also transmute() which drops existing variables |
| mtcars[,c(“mpg”, “am”)], subset() | select(), rename() | see also pull() |
| mtcars[mtcars$am == 1,], subset() | filter()
@kaz-yos
kaz-yos / tableone_openxlsx.R
Created September 22, 2017 17:46
Exporting tableone results to a formatted xlsx file via openxlsx
### Turn tableone output matrix into tidyverse data_frame
tableone_mat_to_data_frame <- function(mat) {
bind_cols(data_frame(Variable = rownames(mat)),
as_data_frame(mat))
}
### Write a xlsx file
write_tableone_mat_to_xlsx <- function(tableone_mat, file) {
## Create a workbook object with one sheet
@kaz-yos
kaz-yos / jpeg.rb
Last active August 7, 2017 13:37
Homebrew Formula for libjpeg version 8d (9b broke my emacs)
class Jpeg < Formula
desc "Image manipulation library"
homepage "http://www.ijg.org"
url "http://www.ijg.org/files/jpegsrc.v8d.tar.gz"
sha256 "00029b1473f0f0ea72fbca3230e8cb25797fbb27e58ae2e46bb8bf5a806fe0b3"
bottle do
cellar :any
rebuild 2
sha256 "85d1f6055de99c1b764c697498bb86d003f5d24b324133f036f0393b97b3e869" => :sierra
@kaz-yos
kaz-yos / AlternativeMIcombine.R
Created February 10, 2017 23:49
Alternative version of mitools:::MIcombine.default
## Implement multivariate version of Rubin's formula
## eq 14 in https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4029775/
## Page 97 of https://www.amazon.com/Multiple-Imputation-Nonresponse-Surveys-Donald/dp/0471655740
AlternativeMIcombine.default <- function (results, variances, call = sys.call(), df.complete = Inf, ...) {
m <- length(results)
oldcall <- attr(results, "call")
if (missing(variances)) {
variances <- suppressWarnings(lapply(results, vcov))
results <- lapply(results, coef)
}
@kaz-yos
kaz-yos / detect-org-babel-languages.el
Created January 30, 2017 02:43
Detect all languages used in org-babel src blocks in a file.
(defun org-babel-get-all-src-block-info ()
"Get information on all src blocks in an org file."
(interactive)
(let ((lst '()))
(save-excursion
;; Go to the beginning of buffer
(beginning-of-buffer)
(while (condition-case err
;; Try to move to the next src block.
(org-next-block nil nil org-babel-src-block-regexp)