Skip to content

Instantly share code, notes, and snippets.

@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 / 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({
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
<!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>
@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")) {
library(rCharts)
.ymax <- 20
.radius <- 5
a <- Highcharts$new()
a$chart(type = "scatter")
a$series(list(
@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()
@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 August 29, 2015 14:03
R: (faster) single value dcast function
require(data.table)
single_val_dcast <- function(data, x, y, value, fill = NA) {
# x . .
# y
# .
# .
if (!is.data.table(data)) {
stop('Input data must be a data.table.')