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_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 / 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 / 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 / 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 / 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 / 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 / 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 / default-company-backends.el
Created May 7, 2018 12:52
Default emacs company-mode backends
(defcustom company-backends `(;; Emacs Lisp
;; In newer versions of Emacs, company-capf is used instead.
,@(unless (version< "24.3.51" emacs-version)
(list 'company-elisp))
;; BBDB https://www.emacswiki.org/emacs/CategoryBbdb
company-bbdb
;; nxml-mode
;; In Emacs >= 26, company-capf is used instead.
,@(unless (version<= "26" emacs-version)
(list 'company-nxml))
@kaz-yos
kaz-yos / mark-every-fifth-files.el
Created June 7, 2018 14:50
Mark every fifth files
(cl-loop
for str in (thread-last
(number-sequence 1 60)
(seq-filter (lambda (x) (zerop (% x 5))))
(seq-map (lambda (x) (concat "000" (number-to-string x))))
(seq-map (lambda (x) (substring x -3)))
(seq-map (lambda (x) (concat "raw" x))))
do (dired-mark-files-regexp str))
@kaz-yos
kaz-yos / tableone_partners_r_group.Rmd
Created July 19, 2018 13:34
Lightning talk at Partners R User Group Meeting on 2018-07-19
---
title: "tableone (Lightning talk at Partners R User Group Meeting)"
author: "Kazuki Yoshida"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---
```{r, message = FALSE, tidy = FALSE, echo = F}
## knitr configuration: http://yihui.name/knitr/options#chunk_options
library(knitr)