Skip to content

Instantly share code, notes, and snippets.

@reinholdsson
reinholdsson / merge_dt.r
Created July 11, 2014 12:58
R: merge multiple data tables
library(data.table)
x <- data.table(A = c(3,2,1), B = 4:6)
y <- data.table(A = 1:4, C = 5:8)
z <- data.table(A = 2:3, D = 5:6)
tbls <- list(x, y, z)
lapply(tbls, function(i) setkey(i, A))
merged <- Reduce(function(...) merge(..., all = T), tbls)
@reinholdsson
reinholdsson / code.r
Last active January 6, 2023 12:12
dplyr helper functions
#' Grouped mutate
#'
#' Group by columns, then mutate, and then regroup to previous groups.
#' @param .data data frame
#' @param .by character vector with columns to group by
#' @param ... arguments passed to mutate(...)
#' @export
grouped_mutate <- function(.data, .by, ...) {
pre <- groups(.data)
if (is.null(pre)) pre <- list()
library(rCharts)
.ymax <- 20
.radius <- 5
a <- Highcharts$new()
a$chart(type = "scatter")
a$series(list(
@reinholdsson
reinholdsson / mailr.r
Last active August 29, 2015 14:01
Send mail through R
library(knitr)
library(mailR)
library(XLConnect)
rmd_to_html <- function(input, output = paste0(tempfile(), ".html")) {
knit2html(input = input, output = output, quiet = T,options="")
return(output)
}
df_to_csv <- function(input, output = paste0(tempfile(), ".csv")) {
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<script src='http://code.jquery.com/jquery-1.9.1.min.js' type='text/javascript'></script>
<script src='http://code.highcharts.com/highcharts.js' type='text/javascript'></script>
<script src='http://code.highcharts.com/highcharts-more.js' type='text/javascript'></script>
<script src='http://code.highcharts.com/modules/exporting.js' type='text/javascript'></script>
a <- Highcharts$new()
a$chart(type = "spline", backgroundColor = NULL)
a$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
a$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 1), dashStyle = "shortdot")
a$legend(symbolWidth = 80)
a$set(height = 250)
a
@reinholdsson
reinholdsson / server.R
Created March 5, 2014 17:47
rCharts - example of Highcharts click events in Shiny
library(shiny)
library(rCharts)
shinyServer(function(input, output) {
output$text <- renderText({
sprintf("The capital of %s is %s.", input$click$country, input$click$capital)
})
output$chart <- renderChart({
@reinholdsson
reinholdsson / coldbir-api-examples.r
Created January 28, 2014 22:55
Coldbir API Examples
library(Coldbir)
a <- cdb(tempfile())
a[] <- mtcars
a[, 2012] <- mtcars
a[, 2013] <- mtcars
a[, c(2012,12)] <- mtcars
a[, c(2012,11)] <- mtcars
# DEFAULT: a[NULL, .all]
@reinholdsson
reinholdsson / knit
Created November 12, 2013 18:42 — forked from baptiste/knit
#!/usr/bin/Rscript
require(knitr)
lf <- commandArgs(trailingOnly = TRUE)
if(!length(lf))
lf = list.files(pattern="\\.rmd")
for (f in lf)
try(knit(f))
md = paste0(sub("\\.[[:alnum:]]*$", "", lf), ".md")
@reinholdsson
reinholdsson / Makefile
Last active December 18, 2020 17:04
RMarkdown to PDF with pandoc (using Makefile)
build:
Rscript build.r
clean:
rm -rf *.tex *.tex-e *.bbl *.blg *.aux *.out *.log *.spl plots/ cache/ *.gz\(busy\) *.gz *.pdf